setTimeOut() or setInterval() . 4 methods to apply same thing. which is best?

后端 未结 3 496
自闭症患者
自闭症患者 2020-12-18 06:09

I am displaying a countdown watch with respect to a given endtime.

although its working perfect but i want to know which is best methods to apply.

below is m

相关标签:
3条回答
  • 2020-12-18 06:52

    I wouldn't use any of your methods. The reason is setTimeout and setInterval do not guarantee that your code will execute after the specified delay. This is because JavaScript is single threaded.

    If I need to call a function only once after a specified delay then I use setTimeout. However if I need to call a function after a fixed interval of time then I do not use setInterval. Instead I make use of delta timing. Here's the code.

    The advantage of using delta timing is that your code will execute closer to the fixed interval of time you specify. It corrects itself. Creating and using a delta timer is simple. For example your code would be written as follows:

    var timer = new DeltaTimer(function (time) {
        $.ajax({
            // properties
        });
    
        if (time - start >= 5000) timer.stop();
    }, 1000);
    
    var start = timer.start();
    

    The above delta timer is better than setInterval (method 1), makes use of setTimeout (method 2) but also corrects itself, starts the timer using a function (method 3), and doesn't pollute the scope with a special clockStart function (method 4).

    In addition you can easily get the exact time the function is called after the timer starts as the time the function is called is passed as an argument to the function. The timer also has a stop method to stop the timer. To start it again call start again.

    Edit:

    If you want to make the DeltaTimer look more like setInterval (start the timer automatically) you may implement a spawn function as follows:

    DeltaTimer.spawn = function (render, interval) {
        var timer = new DeltaTimer(render, interval);
    
        var start = timer.start = function (start) {
            return function () {
                render.start = start();
            };
        }(timer.start);
    
        start();
    
        return timer;
    };
    

    Then you may automatically create and start the DeltaTimer as follows:

    var timer = DeltaTimer.spawn(function countdown(time) {
        $.ajax({
            // properties
        });
    
        if (time - countdown.start >= 5000) timer.stop();
    }, 1000);
    

    Thus var timer = DeltaTimer.spawn(funct, delay); is equivalent to var interval = setInterval(funct, delay); and timer.stop(); is equivalent to clearInterval(interval);. I guess that's as much as you can automate it.

    0 讨论(0)
  • 2020-12-18 07:08

    The benefit of using #1 over #2 is that the window reference removes the chance of a scope variable overwriting setInterval.

    // When out of global scope...
    function setInterval() {
    
    }
    
    window.setInterval(foo, 100); // still calls the "correct" setInterval
    

    There's no difference between wrapping the call to countdown in a function (#1, #2). #2 gives you greater flexibility as you can also call other functions/ pass arguments etc (although it's obviously trivial to swap from #1 to #2 if this becomes the case).

    #4 saves you having to declare a function clockStart, other than that, it's the same as #3.

    Use clearTimeout if you used setTimeout, and clearInterval if you used setInterval...

    You should also be aware of how setTimeout and setInterval work differently. There's an amazing answer here which explains that...

    As for what I'd use? I'd use #2.

    0 讨论(0)
  • 2020-12-18 07:08

    if you are creating countdown then why u don't use jquery plugin and customize it according to your requirements? Checkout here

    http://www.tripwiremagazine.com/2012/05/jquery-countdown-scripts.html

    0 讨论(0)
提交回复
热议问题