javascript 每 9 秒循环一次?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13586008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript loop every 9 seconds?
提问by Boaz Katzir
i am developing a html5 app and i want that after 8 seconds of writing the random place it will write if you have won or not for one second and then it will begin from creating a new random, i've tried that with for but it do'esnt seem to make anything and if i write while(true) the browser crashes.
我正在开发一个 html5 应用程序,我希望在写入随机位置 8 秒后,如果您赢了与否,它会写一秒钟,然后它将从创建一个新的随机数开始,我已经尝试过,但是它似乎没有做任何事情,如果我写 while(true) 浏览器崩溃。
is there any way to fix it?
有什么办法可以解决吗?
jQuery(document).ready(function(){
ImageClicked = false;
for (var i=0;i<8;i++){
var XYScore = 0;
var RandomPlace=Math.floor((Math.random()*10)+1);
var Place;
var WantedXPr;
var WantedYPr;
switch(RandomPlace){
case 1:
Place="Berlin";
WantedYPr=790;
WantedXPr=4300;
break;
case 2:
Place="New York";
WantedYPr=1061;
WantedXPr=2345;
break;
case 3:
Place="barcelona";
WantedYPr=1049;
WantedXPr=4046;
break;
case 4:
Place="Johannesburg";
WantedYPr=2546;
WantedXPr=4618;
break;
case 5:
Place="shanghai";
WantedYPr=1272;
WantedXPr=6664;
break;
case 6:
Place="Moskau";
WantedYPr=732;
WantedXPr=4800;
break;
case 7:
Place="kahir";
WantedYPr=4690;
WantedXPr=1310;
break;
case 8:
Place="Delhi";
WantedYPr=1323;
WantedXPr=5707;
break;
case 9:
Place="rio de genero";
WantedYPr=2478;
WantedXPr=3050;
break;
case 10:
Place="Tokyo";
WantedYPr=1180;
WantedXPr=7102;
break;
}
setTimeout(function(){
if (ImageClicked==false){
$('#HeaderAfterWrite').html(", all the people were killed");
$('#HeaderWrite').html("No one helped ");}
else if(XYScore>69)
$('#HeaderWrite').html("Youv'e succesful recover the city ");
else if(XYScore>39)
$('#HeaderWrite').html("The Parvars are not all of ");
else {$('#HeaderAfterWrite').html("is full destroyed now!");
$('#HeaderWrite').html(" ");}
}, 8000);
$('#Place').html(Place);
}
});
回答by Dineshkani
回答by iamwhitebox
This is more elegant.
这样更优雅。
(function loop() {
setTimeout(function () {
// execute script
loop()
}, 9000);
}());
回答by ArjanSchouten
yes there is a way to fix it.
是的,有办法解决它。
make a function of the timeout.
使超时的功能。
function setTimeoutAgain()
{
var t = setTimeout(function()
{
setTimeoutAgain();
},8000);
}