Skip to content

Instantly share code, notes, and snippets.

@sagiavinash
Last active August 29, 2015 14:20
Show Gist options
  • Save sagiavinash/018c66d7fd34312a555c to your computer and use it in GitHub Desktop.
Save sagiavinash/018c66d7fd34312a555c to your computer and use it in GitHub Desktop.
Immediately invoked setInterval
/* Instead of
interval();
setInterval(interval, 1000);
*/
// -1. Invocation function already defined.
// Never use this even if it looks awesome implicit eval is slow because of an additional parse step.
function interval(){
console.log("Invoked");
}
setInterval("interval()", 1000);
// 1. Invocation function already defined.
function interval(){
console.log("Invoked");
}
setInterval((interval() || interval), 1000); //as setInterval fn's are not mearnt to return. interval() always evaluates to undefined.
// 2. Invocation function declared there itself.
setInterval((function interval() {
console.log("Invoked");
return interval;
})(), 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment