Add Closure to your SetTimeout

Had a quick problem with getting my setTimout() function to run. I will usually run the function like w3schools recommends:

function hello () {
   alert("Hello World");

setTimeout("hello", 5000);

However, I couldn’t get it to run with a jQuery .get script inside the function. However, when I tried an alternate syntax called “closure”:

setTimeout(function () {
   alert("Hello World");
}, 5000);

It worked straight away. Just food for thought, plus it makes your code alot easier to read.

Leave a Reply

Your email address will not be published. Required fields are marked *