所以,我有一个localhost网站,你可以按一个按钮开始一个while循环。我的目的是循环这个while循环和"等待"从其他按钮点击按钮,直到135秒过去。然后,它应该退出循环。当执行代码(嵌入在html中)时,它只能打印" test1",而无法离开循环并打印test2。
allyTrack = 0;
scaleTrack = 0;
enemyTrack = 0;
allyString = ""
scaleString = ""
enemyString = ""
allyClicked = false;
scaleClicked = false;
enemyClicked = false;
function allyAdd(){
allyClicked = true;
}
function scaleAdd(){
scaleClicked = true;
}
function enemyAdd(){
enemyClicked = true;
}
//Making the long vectors
function startGame(){
var startTime = new Date().getTime();
//var currentTime = new Date().getTime();
while(true){
document.getElementById('test').innerHTML = 'test1';
if(allyClicked){
allyTrack += 1;
allyString.append(allyTrack.toString())
allyClicked = false;
}
else if(scaleClicked){
scaleTrack += 1;
scaleString.append(scaleTrack.toString())
scaleClicked = false;
}
else if(enemyClicked){
enemyTrack += 1;
enemyString.append(enemyTrack.toString())
enemyClicked = false;
}
else{
allyString.append(allyTrack.toString())
scaleString.append(scaleTrack.toString())
enemyString.append(enemyTrack.toString())
}
currentTime = new Date().getTime();
if(currentTime-startTime > 2000){
break;
}
}
document.getElementById('test').innerHTML = 'test2';
document.getElementById('list').innerHTML = allyString + '\n' + scaleString + '\n' + enemyString;
}
答案 0 :(得分:3)
String.append()不属于JavaScript。我将其替换为concat(),现在可以使用。
答案 1 :(得分:2)
您可以使用全局setTimeout
方法:
function loop(callback, interval) {
return setTimeout(function() {
callback();
}, +interval);
}
loop(function() {
console.log("Looping...");
}, 1000);