In my javascript code i have the following:
function foo(param1) {
setInterval( bar(param1), 3000 );
}
function bar(msg) {
alert(msg);
}
function set() {
foo('hello');
}
And on page load i call set(), it gives the alert message only once. Why it does not work as i would expect.? I think the problem is with the parameter that i am passing the foo() function, but what it is exactly, i am not able to figure out.
If the problem is with the parameter, what can be an alternate way to achieve the desired result.?
Thanks.
You need to pass a function to
setInterval, you are executing a function and passing it’s return value (undefinedsince the function executed does not have a return value).Try using an anonymous function instead:
param1never changes here, so there is no need for anything more complicated. Here’s a working example.