在下面的代码段中,我希望可以在waitingTime上设置timeDiff。但是我知道内部函数内部的timeDiff仅在执行时才有价值。如何使用元素的TIMEDIFF为Give函数设置超时?
var timeDiff;
for (var i = 0; i < q.length; i++) {
(function() {
timeouts.push($timeout(function() {
var element = q.shift(); // q is a queue
timeDiff = element.TIMEDIFF;
console.log(element.TIMEDIFF); //3000
broadcastData(element);
}, waitingTime));
// console.log(timeDiff + " timeiff");
waitingTime = waitingTime + timeDiff; // 1000 + 3000
})();
}
简单来说,waitingTime应该取决于element.TIMEDIFF。如何实现呢?
最佳答案
我做了些改动,希望对您有用。
var timeDiff;
for (var i = 0; i < q.length; i++) {
var element = q[i];
timeDiff = element.TIMEDIFF;
timeouts.push(setTimeout(function(data) {
broadcastData(data.e);
}, waitingTime, {e:element}));
waitingTime = waitingTime + timeDiff;
}
关于javascript - 访问超时函数中的变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40428640/