[英]Passing variable to setInterval function
Been trying to do this over and over again and but my variable just outputs undefined in the div "current". 试图一遍又一遍地执行此操作,但是我的变量只是在div“ current”中输出未定义。 So I want to pass a "changing" variable from "function one" to "function two".
所以我想将一个“更改中的”变量从“功能一”传递给“功能二”。 Tried to set up a simple example :)
试图建立一个简单的例子:)
<div id="current"></div>
<script>
id = 0;
setInterval(function one(){
id++;
name = 'id' + id;
},1000);
setInterval(function two(){
document.getElementById("current").innerHTML=name; //outputs undefined
},10);
</script>
http://jsfiddle.net/pPk4y/ it works? http://jsfiddle.net/pPk4y/有效吗? click run on link.
点击在链接上运行。 as Sirko said as there is no var in front of 'name' it becomes a global variable
正如Sirko所说,因为“名称”前没有var,它成为全局变量
id = 0;
setInterval(function one(){
id++;
name = 'id' + id;
},1000);
setInterval(function two(){
document.getElementById("current").innerHTML=name; //outputs undefined
},10);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.