在所有代码运行之前完成一个功能的噩梦。我正在尝试构建一个计数器,并且仅在代码完成时返回。
我已经这样模仿了(我知道这并不好,但如果有人能指出我正确的路线,我将非常感激):
//I want this to alert "Done"
alert(timerCheck());
function timerCheck() {
var finished;
var myLoop = 5;
for (i = 0; i < myLoop; i++) {
//This is emulating the slow code
window.setTimeout(checkFinished, 900);
alert(i);
}
function checkFinished() {
//I originally had the "return here, before realising my error
finished = true;
}
if (finished) {
//This is where my problem is
return "done";
}
}
就像我说的,一个非常简化的例子——如果有人能指出错误,它会为我省去很多麻烦!