The following function shows the alert every 5 seconds:
function foo() {
bar="foo";
alert (bar);
setTimeout(foo, 5000);
}
foo();
However, when I add arguments to the function and call from setTimeout
, it no longer waits 5 seconds, it just alerts endlessly without any delay:
function foo(bar) {
bar="foo";
alert (bar);
setTimeout(foo(bar), 5000);
}
foo();
Why is this and how can I loop through the function with a delay while passing arguments?