Say I have some code:
setInterval(foo,1000); // here is an interval that causes a function to repeat
function foo(){
//a number of lines of code
}
Now, i think of this as causing foo() to be executed every second by the system clock, whereas this:
function foo(){ //a number of lines of code setTimeout(foo,1000); } //here is a timeout that causes the function to repeat
Am I correct in guessing that the latter function would be executed less often in a given period of time, because I’m inserting a pause between executions (which, themselves, take a measurable amount of time)? Any further points or enlightenment on this mechanism would be welcomed.
Let me quote an article about timers by John Resig, which specifically addresses your question:
Keep also in mind that accuracy of JavaScript timers differs between browsers and platforms.