我有一个localhost网站,我有一个" start"触发跟随功能的按钮:
function startGame(){
startTime = new Date().getTime();
currentTime = new Date().getTime();
while((currentTime-startTime)<2000){
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();
}
document.getElementById('test').innerHTML = 'test2';
document.getElementById('list').innerHTML = allyString + '\n' + scaleString + '\n' + enemyString;
}
我似乎遇到的问题是,虽然我设置了2000毫秒的时间限制,但它不会离开循环。 (并改变&#34; test1&#34;到&#34; test2&#34;)客户端 - 服务器通信让我感到困惑,因为这是我第一次尝试访问网站,所以如果我错过了某些内容,我会道歉哑。
编辑:
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((Math.abs(new Date().getTime() - startTime))>2000){
break;
}
}
document.getElementById('test').innerHTML = 'test2';
document.getElementById('list').innerHTML = allyString + '\n' + scaleString + '\n' + enemyString;
}
仍未打印&#34; test2&#34;。我一定还在遗漏一些东西。
答案 0 :(得分:-1)
在您的while循环条件下,您是否尝试过切换订单?像这样:
while (startTime - currentTime < 2000) {
// code goes here
}