Difference Between setTimeout and setInterval

Difference Between setTimeout and setInterval

People may ask, what is the Difference Between setTimeout and setInterval. Both of them have a similar function, that is triggering another function after period of time. However, good programmer have to know the Difference Between setTimeout and setInterval.

In this case, setTimeout has a function to trigger another function after a period of time when the setTimeout is triggered. However, the setInterval will trigger another function after a period of time when the setInterval is triggered, and will trigger the other function after a period of time when the other function triggered previously.

setTimeout will only trigger another function once, while setInterval will continously trigger the other function.

Exp (code in Javascript for setTimeout):

setTimeout('alert("Hello Me!");', 1000);//1000 means 1000 ms

The above code will alert “Hello Me!” one second after the page is load.

Exp (code in Javascript for setInterval):

setInterval('alert("Hello Me!");', 1000);//1000 means 1000 ms

The above code will alert “Hello Me!” one second after the page is load, and every one second after you close the alert.

So far, that is the only Difference Between setTimeout and setInterval that I know. If you know another differences, fell free to tell me.

Good Luck

http://septiadi.com/2011/03/07/difference-between-settimeout-and-setinterval/


Leave a comment