简体   繁体   English

JavaScript:使用setInterval()将变量从一个函数传递到另一个函数?

[英]JavaScript: Pass a variable from one function to another, using setInterval()?

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: 我希望能够在参数f中执行该函数并设置一些变量(例如变量 ),以便该函数可以使用它们,总的来说这是一个想法:

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 . 浏览器可能只是忽略错误并提醒undefined

But anyways, how do I "pass" the variable without changing the scope of it . 但无论如何,如何在不改变变量范围的情况下 “传递”变量。

JavaScript has closures , so you can do this: JavaScript有闭包 ,所以你可以这样做:

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. setInterval接受两个参数,第二个参数是您希望延迟执行传入函数引用的时间量(以毫秒为单位)。

If you prefer to have a function without parameter, you could consider using the call or apply method: 如果您更喜欢没有参数的函数,可以考虑使用callapply方法:

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

Given that the lambda function is coming from another scope, you can't use a closure to get the value in the function directly. 鉴于lambda函数来自另一个范围,您不能使用闭包直接获取函数中的值。 You'll have to pass the value to your function directly via a closure, and be prepared to receive it in the lambda: 您必须通过闭包直接将值传递给函数,并准备在lambda中接收它:

function say(f){
   var variable = "hey";
   setInterval( function(){ f(variable) }, 500 );
}
say(function(yyy){
   alert(yyy);
});
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. 通过在函数上使用call方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript:将数字变量从函数传递到setInterval - Javascript: Pass number variable from a function to setInterval 如何在javascript中将变量从一个函数传递给另一个函数 - how to pass variable from one function to another function in javascript 如何在JavaScript中将局部变量从一个函数传递给另一个函数 - how to pass local variable from one function to another function in javascript 将变量从一个JavaScript回调匿名函数传递给另一个 - Pass variable from one JavaScript callback anonymous function to another 如何将变量从一个 function 传递到另一个 Javascript? - How to pass variable from one function to another Javascript? 我可以将变量从一个 function 传递到另一个 javascript 吗? - Can I pass variable from one function to another in javascript? Javascript将变量值​​从一个函数传递给另一个函数 - Javascript pass variable value from one function to another 无法将变量从一个函数传递给Javascript中的另一个函数 - Not able to pass variable from one function to another in Javascript 您可以使用HTML将Javascript变量从一个函数传递给另一个函数吗 - Can you pass Javascript Variable from one function to another using HTML 如何使用 JavaScript 将变量从一个函数传递到另一个函数 - How to pass variable from one function to another, both with parameters using JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM