1

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.

3 Answers 3

2

The best way is to use a closure to access the parameter from a function that you pass to setInterval:

function foo(param1) {
    setInterval(function () {
        bar(param1);
    }, 3000);
}

function bar(msg) {
    alert(msg);
}

function set() {
    foo('hello');
}
Sign up to request clarification or add additional context in comments.

7 Comments

For my daily dose of pedantry, there are no closures in this code. From the question you linked to: "That is not a closure. A closure is when you return the inner function. The inner function will close-over the variables of foo before leaving."
@jbabey Fair enough, I stand corrected. :) So I can correct the answer, what's the proper term for a function accessing variables from its parent context?
@PhilBooth it's just an anonymous function declaration declared inside a named (foo) function declaration.
@jbabey Actually, having read the comments to that answer, the other answers to that question and the article about closures on wikipedia, I disagree. :) It's not necessary to return the function for it to be a closure, only that the function runs and has access to the enclosing context after the parent function has already returned. Which it does, in this case.
@pratik That scenario is fine. The way to think about it is that you are defining a brand new function every time you invoke foo and those functions all have perpetual, dynamic access to their context. A second invocation of foo in your case will create a second closure, which accesses a second context. The scenario to be careful of is when you want to define a closure inside the body of a loop. In that case you must call a function outside the body of the loop, fixing the context of each loop iteration, otherwise every closure will refer to a single context (the terminal loop iteration).
|
1

You need to pass a function to setInterval, you are executing a function and passing it's return value (undefined since the function executed does not have a return value).

Try using an anonymous function instead:

function foo(param1) {
    setInterval(function () {
        bar(param1);
    }, 3000);
}

function bar(msg) {
    alert(msg);
}

param1 never changes here, so there is no need for anything more complicated. Here's a working example.

1 Comment

Thanks. So its Anonymous function, not a closure.
1

setInterval expects a function while you're giving the return value of calling the whole function.

A modified version that should work:

var msg = null;

function foo(param1) {
   // When foo is called, its argument is set to msg variable, so the function called 
   // by setInterval will have access to the message that must show in a window.alert
    msg = param1;
    setInterval(bar, 3000 );
}

function bar() {
    alert(msg);
}

function set() {
    foo('hello');
}

7 Comments

While this will work with the global variable, i want bar(msg) to alert exactly the parameter msg's value that i have passed to it.
@patrik You're not right. This approach does what you want. Other discussion is that the closure approach is better, but I just wanted to show you were your code was wrong and a basic hint on how to get it working.
Yes, your answer is right and it does the job, but what if i do not want to create a global variable.? So in that case i will have to use the anonymous function approach.
@pratik In my devs, I would never use global vars. I thought you could
Nope, even i do not want to use global vars. I should have been more specific about that in the question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.