3

So I have a function that loops executing a function, example:

function(f){
  var variable;
  for(z = 0; z < 10; z++){
    variable = "cool";         
    setInterval(f)
  }

Btw, the real function is MUCH more complex than this but it's the same theory. I want to be able to execute the function in argument f and set some variables (ex. variable) so that this function can use them, in a whole this is the idea:

function say(f){
   var variable = "hey";
   setInterval(f);
}
say(function(){
   alert(variable)
});

Here, one should get an alert box saying hey. That's the theory, but it wont work:

Variable "variable" isn't defined

The browser will probably just ignore the error and alert undefined.

But anyways, how do I "pass" the variable without changing the scope of it.

4 Answers 4

7

JavaScript has closures, so you can do this:

var x = 0;
setInterval(function() {
   //do something with x
}, 1000);

In your specific case, you should do this:

function say(f){
   var variable = "hey";
   setInterval(function() { 
       f(variable); //invoke the say function's passed-in "f" function, passing in "hey"
   }, 1000);
}
say(function(arg){
    //arg will be "hey"
    alert(arg);
});

setInterval takes two arguments, the second being the amount of time (in milliseconds) that you wish to delay execution of the passed-in function reference.

Sign up to request clarification or add additional context in comments.

1 Comment

I know the setInterval takes two arguments. Setting the second one to zero is almost the same as not setting it, it will execute in no amount of time. Just a easy way of executing things, even though there are other (probably better) ways.
3

If you prefer to have a function without parameter, you could consider using the call or apply method:

function say(f){
   var variable = "hey";
   setInterval(function() { 
       f.call(variable); // set the context
   }, 1000);
}
say(function(){
    alert(this);
});

1 Comment

This is, in my opinion, the BEST method, thanks! The other ones are almost the same, but this one just looks cleaner, and is exactly what I needed.
1

Given that the lambda function is coming from another scope, you can't use a closure to get the value in the function directly. You'll have to pass the value to your function directly via a closure, and be prepared to receive it in the lambda:

function say(f){
   var variable = "hey";
   setInterval( function(){ f(variable) }, 500 );
}
say(function(yyy){
   alert(yyy);
});

1 Comment

What's lambda lol, anyways, it's a good method even though I prefer not to rely on the existence of an argument at "the other side" (in this case yyy)
1
function say(f){
  var variable = "hey";
  setInterval(function(){
    f.call(null, variable);
  }, 1000);
}
say(function(variable){
   alert(variable)
});

By using the call method on the function.

2 Comments

Fixed it now; although does not look clean anymore.
Wise, even though f(variable) is cleaner ;) EDIT: Even though I'll end up using the call method

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.