Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does this "delay" function works

Tags:

javascript

I am using this code to wrap parts of code in it is used like this,

var delay = (function() {
    // SET TIMER
    var timer = 0;
    // RETURN SET TIMEOUT FUNCTION
    return function(callback, ms) {
        clearTimeout(timer);
        timer = setTimeout(callback, ms);
    };
})();​

I call it like this,

delay(function() {
     .......
}, 1000);

And it will delay it by 1000 milliseconds, but I do not understand what is going on thanks :)

like image 852
cgwebprojects Avatar asked Apr 08 '12 22:04

cgwebprojects


People also ask

How does delay function work in Arduino?

The delay() function allows you to pause the execution of your Arduino program for a specified period. For that purpose, the method requires you to supply it with a whole number that specifies how many milliseconds the program should wait.

How delay is used in processing?

Description. The delay() function causes the program to halt for a specified time. Delay times are specified in thousandths of a second. For example, running delay(3000) will stop the program for three seconds and delay(500) will stop the program for a half-second.

What is the use of delay in computer graphics?

delay(): The delay() function in C is used to stop the execution of the program for some period of time. Parameters: It accepts a time in milliseconds to stop the execution of the program to that period of time.

What parameter does the delay () function requires?

The Delay function requires a numeric value as a parameter. This value is a whole number that tells JAWS the length of time to pause script execution. This value can be a numeric value such as 5, an integer variable containing a value or a constant representing a numeric value.


1 Answers

The delay is a function that will return another function. The timer variables is inside the closure of the delay function so it can still be accesed by the returning function. The function. You could also write it like this

var delay;
var timer = 0;
delay = function(callback, ms) {
    clearTimeOut(timer);
    timer = setTimeout(callback, ms);
}

The problem that you have now is that if you call delay twice it will overwrite the timer variables so the second delay will overwrite the timer variable. I tested this out and it seems that your function is also broken it should be:

var delay = function(){
// SET TIMER
    var timer = 0;
// RETURN SET TIMEOUT FUNCTION
    return function(callback, ms){
        clearTimeout(timer);
        timer = setTimeout(callback, ms);
    };
};

delay()(function(){console.log("hello1");}, 5000);
delay()(function(){console.log("hello2");}, 5000);

If your code does the same it will only trace hello2 because the first one will overwrite the timer variable.

Unless your intention is that a second delay will stop the first delay you should use a different approuch.

like image 106
automaticoo Avatar answered Oct 30 '22 04:10

automaticoo