delay method

In Javascript, setTimeout() is used to delay a function or an expression after a specified number of milliseconds. To delay a function, it's usually expressed as,

var t = setTimeout(foo, 1000);
 
function foo() {
    alert("Hello!");
}

The function foo() is fired after 1 seconds.

Here we could extend the prototype property of the native object Function and get an extended method - delay().

Function.prototype.delay = function(del) {
    var _this = this;
    (function() { 
    setTimeout(_this,del);
    })();
}

Now we can express the above code as,

foo.delay(1000);

That's it. A simple method that could ne used in Javascript animation.

Social Bookmark if it is useful.