Named Self Invoking Function
I use this pattern for polling data from servers periodically. This makes the code less clutter (especially setTimeout
line).
(function poll() {
$.get("/somedata", function (data) {
// Processing data...
setTimeout(poll, 1000);
});
})();
If you needed a recursive self-invoking function then it may make sense:
(function loop(i) {
console.log(i);
i++;
if(i < 10) {
loop(i);
}
})(0);