repeat method

In Javascript, setInterval() is used to repeat a function or an expression in a specified number of milliseconds interval. To repeat a function, it's usually expressed as,

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

The function foo() is repeated in 1000 milliseconds interval.

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

Function.prototype.repeat = function(del) {
    var _this = this;
    (function() { 
    t = setInterval(_this,del);
    })();
}

Now we can express the above code as,

foo.repeat(1000);

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

Social Bookmark if it is useful.