Repeating setTimeout

前端 未结 7 848
太阳男子
太阳男子 2020-12-15 15:56

I am trying to repeat setTimeout every 10 seconds. I know that setTimeout by default only waits and then performs an action one time. How can I rep

相关标签:
7条回答
  • 2020-12-15 16:16

    Maybe you should use setInterval()

    0 讨论(0)
  • 2020-12-15 16:17

    according to me setInterval() is the best way in your case.
    here is some code :

     setInterval(function() {
    
    //your code
    
    }, 10000); 
    // you can change your delay by changing this value "10000".
    
    0 讨论(0)
  • 2020-12-15 16:19
    const myFunction = () => {
            setTimeout(() => {
                document.getElementById('demo').innerHTML = Date();
                myFunction();
            }, 10000);
        }
    

    Easiest, but not efficient way!

    0 讨论(0)
  • 2020-12-15 16:21

    setInterval() is probably what you're looking for, but if you want to do get the same effect with setTimeout():

    function doSomething() {
        console.log("10 seconds");
        setTimeout(doSomething, 10000);
    }
    
    setTimeout(doSomething, 10000);
    

    Or if you don't want to declare a separate function and want to stick with a function expression you need to make it a named function expression:

    setTimeout(function doSomething() {
        console.log("10 seconds");
        setTimeout(doSomething, 10000);
    }, 10000);
    

    (Or use arguments.callee if you don't mind using deprecated language features.)

    0 讨论(0)
  • 2020-12-15 16:24

    Using jQuery, this is what you could do:

    function updatePage() {
    
    var interval = setTimeout(updatePage, 10000); // 10' Seconds
    
        $('a[href]').click(function() {
          $(this).data('clicked', true);
          clearInterval(interval); // Clears Upon Clicking any href Link
          console.log('Interval Cleared!');
        });
       // REPLACE 'YOUR_FUNCTION_NAME' function you would like to execute
        setTimeout(YOUR_FUNCTION_NAME, 500);
    
    } // Function updatePage close syntax
    
    updatePage(); // call the function again.

    0 讨论(0)
  • 2020-12-15 16:25

    Unlike the answers provided by @nnnnnn and @uzyn I discourage you from making use of setInterval for reasons elaborated in the following answer. Instead make use of the following Delta Timing script:

    function DeltaTimer(render, interval) {
        var timeout;
        var lastTime;
    
        this.start = start;
        this.stop = stop;
    
        function start() {
            timeout = setTimeout(loop, 0);
            lastTime = + new Date;
            return lastTime;
        }
    
        function stop() {
            clearTimeout(timeout);
            return lastTime;
        }
    
        function loop() {
            var thisTime = + new Date;
            var deltaTime = thisTime - lastTime;
            var delay = Math.max(interval - deltaTime, 0);
            timeout = setTimeout(loop, delay);
            lastTime = thisTime + delay;
            render(thisTime);
        }
    }
    

    The above script runs the given render function as close as possible to the specified interval, and to answer your question it makes use of setTimeout to repeat a process. In your case you may do something as follows:

    var timer = new DeltaTimer(function (time) {
        console.log("10 seconds");
    }, 10000);
    
    var start = timer.start();
    
    0 讨论(0)
提交回复
热议问题