PeterP
PeterP

Reputation: 71

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". 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>

Upvotes: 1

Views: 1524

Answers (1)

Dere_2929
Dere_2929

Reputation: 365

http://jsfiddle.net/pPk4y/ it works? click run on link. as Sirko said as there is no var in front of 'name' it becomes a global variable

id = 0;
setInterval(function one(){
id++;
name = 'id' + id;
},1000);


setInterval(function two(){

    document.getElementById("current").innerHTML=name; //outputs undefined

 },10);

Upvotes: 1

Related Questions