简体   繁体   中英

Scope of global variable inside setInterval

function data(){
    var dataval = anyValue;
    var myVar = setInterval(function(){computeData()});
    function computeData(){
      //inside this function there will be computations 
      //where dataval is included
    }
 }

This is just an example. My problem is that when I update the value of dataval and call the data(), the value is not updated inside the setInterval function. Unless I change the value again and call data(). But that is not even working well.

anyValue  = 10

//the computation works

//if i update it to decreasingly, anyValue = 9, //it still uses the value 10,

//then if i use anyValue = 8, now it uses the value 9

This is not the code I am using. But same logic. Thanks! I hope you get my question! `

将您的计算数据函数computeData(object) data()之外,并使其接受变量,例如computeData(object)然后将所需的对象通过var myVar = setInterval(function(){computeData(dataval)});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM