[英]I can't change global variable inside setInterval es6
我必须找到一个按钮,当它出现时。 为此,我使用setInterval。 当找到该按钮时,它将给我变量所需的值。 我在setTimeout中检查它,但是在setTimeout(这些方法之外)之后,我的全局变量变为与setTimeout之前一样。 如何解决?
let foundValue;
function findById(id) {
let interval = setInterval(() => {
if (document.getElementById(id)){
let foundValue = document.getElementById(id);
clearInterval(interval);
}
}, 1000);
return foundValue;
}
这是因为您要在setInterval
重新声明foundValue
,因此应删除第二个let
,例如:
let foundValue;
function findById(id) {
let interval = setInterval(() => {
if (document.getElementById(id)){
foundValue = document.getElementById(id);
clearInterval(interval);
}
}, 1000);
return foundValue;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.