Simply trying to scroll down, every 1 second, in javascript










0















I am trying to use the setTimeout function 3 times, but for some reason it does not work. I was under the impression that I had to use the clearTimeout:



handle = setTimeout(function()
window.scrollBy(0, 1000);
, 1000);
window.clearTimeout(handle)

handle = setTimeout(function()
window.scrollBy(0, 1000);
, 1000);
window.clearTimeout(handle)

handle = setTimeout(function()
window.scrollBy(0, 1000);
, 1000);
window.clearTimeout(handle)


But this doesn't work. I'm not sure why. I'm learning out the timeout handle every time before running the next one. What am I doing wrong here?



EDIT



Using the setInterval() function results in me getting another error. Here's the example:



var myVar = setInterval(scrollDown(), 5000);

function scrollDown()
window.scrollBy(0,1500);



and the error from the console:




Refused to evaluate a string as JavaScript because 'unsafe-eval' is
not an allowed source of script in the following Content Security
Policy directive: "script-src 'report-sample'
'sha256-6gLjSWp3GRKZCUFvRX5aGHtECD1wVRgJOJp7r0ZQjV0='




So I get that there's a Content Security Policy enabled, but what does scrolling down have to do with this? Not sure why this is happening.










share|improve this question



















  • 2





    All of your timeouts start at the same time and are cleared immmediately after they are started. The rest of the code doesn't wait for the timeout to complete before executing. You have to not clear them, then declare the next one inside the current, or use an interval

    – Tiny Giant
    Nov 14 '18 at 23:09












  • Thanks @TinyGiant. Much appreciated. Going to try with the setInterval route.

    – LewlSauce
    Nov 14 '18 at 23:20











  • You're assigning the result of scrollDown() as the callback for setTimeout. It should be setInterval(scrollDown, 5000)

    – Tiny Giant
    Nov 14 '18 at 23:27











  • That did the trick. Thanks man! Did not know about this. Much appreciated!

    – LewlSauce
    Nov 14 '18 at 23:27















0















I am trying to use the setTimeout function 3 times, but for some reason it does not work. I was under the impression that I had to use the clearTimeout:



handle = setTimeout(function()
window.scrollBy(0, 1000);
, 1000);
window.clearTimeout(handle)

handle = setTimeout(function()
window.scrollBy(0, 1000);
, 1000);
window.clearTimeout(handle)

handle = setTimeout(function()
window.scrollBy(0, 1000);
, 1000);
window.clearTimeout(handle)


But this doesn't work. I'm not sure why. I'm learning out the timeout handle every time before running the next one. What am I doing wrong here?



EDIT



Using the setInterval() function results in me getting another error. Here's the example:



var myVar = setInterval(scrollDown(), 5000);

function scrollDown()
window.scrollBy(0,1500);



and the error from the console:




Refused to evaluate a string as JavaScript because 'unsafe-eval' is
not an allowed source of script in the following Content Security
Policy directive: "script-src 'report-sample'
'sha256-6gLjSWp3GRKZCUFvRX5aGHtECD1wVRgJOJp7r0ZQjV0='




So I get that there's a Content Security Policy enabled, but what does scrolling down have to do with this? Not sure why this is happening.










share|improve this question



















  • 2





    All of your timeouts start at the same time and are cleared immmediately after they are started. The rest of the code doesn't wait for the timeout to complete before executing. You have to not clear them, then declare the next one inside the current, or use an interval

    – Tiny Giant
    Nov 14 '18 at 23:09












  • Thanks @TinyGiant. Much appreciated. Going to try with the setInterval route.

    – LewlSauce
    Nov 14 '18 at 23:20











  • You're assigning the result of scrollDown() as the callback for setTimeout. It should be setInterval(scrollDown, 5000)

    – Tiny Giant
    Nov 14 '18 at 23:27











  • That did the trick. Thanks man! Did not know about this. Much appreciated!

    – LewlSauce
    Nov 14 '18 at 23:27













0












0








0








I am trying to use the setTimeout function 3 times, but for some reason it does not work. I was under the impression that I had to use the clearTimeout:



handle = setTimeout(function()
window.scrollBy(0, 1000);
, 1000);
window.clearTimeout(handle)

handle = setTimeout(function()
window.scrollBy(0, 1000);
, 1000);
window.clearTimeout(handle)

handle = setTimeout(function()
window.scrollBy(0, 1000);
, 1000);
window.clearTimeout(handle)


But this doesn't work. I'm not sure why. I'm learning out the timeout handle every time before running the next one. What am I doing wrong here?



EDIT



Using the setInterval() function results in me getting another error. Here's the example:



var myVar = setInterval(scrollDown(), 5000);

function scrollDown()
window.scrollBy(0,1500);



and the error from the console:




Refused to evaluate a string as JavaScript because 'unsafe-eval' is
not an allowed source of script in the following Content Security
Policy directive: "script-src 'report-sample'
'sha256-6gLjSWp3GRKZCUFvRX5aGHtECD1wVRgJOJp7r0ZQjV0='




So I get that there's a Content Security Policy enabled, but what does scrolling down have to do with this? Not sure why this is happening.










share|improve this question
















I am trying to use the setTimeout function 3 times, but for some reason it does not work. I was under the impression that I had to use the clearTimeout:



handle = setTimeout(function()
window.scrollBy(0, 1000);
, 1000);
window.clearTimeout(handle)

handle = setTimeout(function()
window.scrollBy(0, 1000);
, 1000);
window.clearTimeout(handle)

handle = setTimeout(function()
window.scrollBy(0, 1000);
, 1000);
window.clearTimeout(handle)


But this doesn't work. I'm not sure why. I'm learning out the timeout handle every time before running the next one. What am I doing wrong here?



EDIT



Using the setInterval() function results in me getting another error. Here's the example:



var myVar = setInterval(scrollDown(), 5000);

function scrollDown()
window.scrollBy(0,1500);



and the error from the console:




Refused to evaluate a string as JavaScript because 'unsafe-eval' is
not an allowed source of script in the following Content Security
Policy directive: "script-src 'report-sample'
'sha256-6gLjSWp3GRKZCUFvRX5aGHtECD1wVRgJOJp7r0ZQjV0='




So I get that there's a Content Security Policy enabled, but what does scrolling down have to do with this? Not sure why this is happening.







javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 23:23







LewlSauce

















asked Nov 14 '18 at 23:07









LewlSauceLewlSauce

1,08611226




1,08611226







  • 2





    All of your timeouts start at the same time and are cleared immmediately after they are started. The rest of the code doesn't wait for the timeout to complete before executing. You have to not clear them, then declare the next one inside the current, or use an interval

    – Tiny Giant
    Nov 14 '18 at 23:09












  • Thanks @TinyGiant. Much appreciated. Going to try with the setInterval route.

    – LewlSauce
    Nov 14 '18 at 23:20











  • You're assigning the result of scrollDown() as the callback for setTimeout. It should be setInterval(scrollDown, 5000)

    – Tiny Giant
    Nov 14 '18 at 23:27











  • That did the trick. Thanks man! Did not know about this. Much appreciated!

    – LewlSauce
    Nov 14 '18 at 23:27












  • 2





    All of your timeouts start at the same time and are cleared immmediately after they are started. The rest of the code doesn't wait for the timeout to complete before executing. You have to not clear them, then declare the next one inside the current, or use an interval

    – Tiny Giant
    Nov 14 '18 at 23:09












  • Thanks @TinyGiant. Much appreciated. Going to try with the setInterval route.

    – LewlSauce
    Nov 14 '18 at 23:20











  • You're assigning the result of scrollDown() as the callback for setTimeout. It should be setInterval(scrollDown, 5000)

    – Tiny Giant
    Nov 14 '18 at 23:27











  • That did the trick. Thanks man! Did not know about this. Much appreciated!

    – LewlSauce
    Nov 14 '18 at 23:27







2




2





All of your timeouts start at the same time and are cleared immmediately after they are started. The rest of the code doesn't wait for the timeout to complete before executing. You have to not clear them, then declare the next one inside the current, or use an interval

– Tiny Giant
Nov 14 '18 at 23:09






All of your timeouts start at the same time and are cleared immmediately after they are started. The rest of the code doesn't wait for the timeout to complete before executing. You have to not clear them, then declare the next one inside the current, or use an interval

– Tiny Giant
Nov 14 '18 at 23:09














Thanks @TinyGiant. Much appreciated. Going to try with the setInterval route.

– LewlSauce
Nov 14 '18 at 23:20





Thanks @TinyGiant. Much appreciated. Going to try with the setInterval route.

– LewlSauce
Nov 14 '18 at 23:20













You're assigning the result of scrollDown() as the callback for setTimeout. It should be setInterval(scrollDown, 5000)

– Tiny Giant
Nov 14 '18 at 23:27





You're assigning the result of scrollDown() as the callback for setTimeout. It should be setInterval(scrollDown, 5000)

– Tiny Giant
Nov 14 '18 at 23:27













That did the trick. Thanks man! Did not know about this. Much appreciated!

– LewlSauce
Nov 14 '18 at 23:27





That did the trick. Thanks man! Did not know about this. Much appreciated!

– LewlSauce
Nov 14 '18 at 23:27












1 Answer
1






active

oldest

votes


















-1














There's no need to clear the timeout, as that will cancel your pending function call. Instead try calling setTimeout with 3 different intervals (1000, 2000, 3000) or use setInterval and cancel after the 3rd call



// setTimeout
setTimeout(function()
window.scrollBy(0, 1000);
, 1000);
setTimeout(function()
window.scrollBy(0, 1000);
, 2000);
setTimeout(function()
window.scrollBy(0, 1000);
, 3000);

// setInterval
var i = 0;
var handle = setInterval(function()
window.scrollBy(0, 1000);
i++;
if (i === 3)
clearInterval(handle);

, 1000);





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310093%2fsimply-trying-to-scroll-down-every-1-second-in-javascript%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    -1














    There's no need to clear the timeout, as that will cancel your pending function call. Instead try calling setTimeout with 3 different intervals (1000, 2000, 3000) or use setInterval and cancel after the 3rd call



    // setTimeout
    setTimeout(function()
    window.scrollBy(0, 1000);
    , 1000);
    setTimeout(function()
    window.scrollBy(0, 1000);
    , 2000);
    setTimeout(function()
    window.scrollBy(0, 1000);
    , 3000);

    // setInterval
    var i = 0;
    var handle = setInterval(function()
    window.scrollBy(0, 1000);
    i++;
    if (i === 3)
    clearInterval(handle);

    , 1000);





    share|improve this answer



























      -1














      There's no need to clear the timeout, as that will cancel your pending function call. Instead try calling setTimeout with 3 different intervals (1000, 2000, 3000) or use setInterval and cancel after the 3rd call



      // setTimeout
      setTimeout(function()
      window.scrollBy(0, 1000);
      , 1000);
      setTimeout(function()
      window.scrollBy(0, 1000);
      , 2000);
      setTimeout(function()
      window.scrollBy(0, 1000);
      , 3000);

      // setInterval
      var i = 0;
      var handle = setInterval(function()
      window.scrollBy(0, 1000);
      i++;
      if (i === 3)
      clearInterval(handle);

      , 1000);





      share|improve this answer

























        -1












        -1








        -1







        There's no need to clear the timeout, as that will cancel your pending function call. Instead try calling setTimeout with 3 different intervals (1000, 2000, 3000) or use setInterval and cancel after the 3rd call



        // setTimeout
        setTimeout(function()
        window.scrollBy(0, 1000);
        , 1000);
        setTimeout(function()
        window.scrollBy(0, 1000);
        , 2000);
        setTimeout(function()
        window.scrollBy(0, 1000);
        , 3000);

        // setInterval
        var i = 0;
        var handle = setInterval(function()
        window.scrollBy(0, 1000);
        i++;
        if (i === 3)
        clearInterval(handle);

        , 1000);





        share|improve this answer













        There's no need to clear the timeout, as that will cancel your pending function call. Instead try calling setTimeout with 3 different intervals (1000, 2000, 3000) or use setInterval and cancel after the 3rd call



        // setTimeout
        setTimeout(function()
        window.scrollBy(0, 1000);
        , 1000);
        setTimeout(function()
        window.scrollBy(0, 1000);
        , 2000);
        setTimeout(function()
        window.scrollBy(0, 1000);
        , 3000);

        // setInterval
        var i = 0;
        var handle = setInterval(function()
        window.scrollBy(0, 1000);
        i++;
        if (i === 3)
        clearInterval(handle);

        , 1000);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 23:16









        dotconnordotconnor

        1,180321




        1,180321





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310093%2fsimply-trying-to-scroll-down-every-1-second-in-javascript%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo