javascript循环每9秒? [英] javascript loop every 9 seconds?

查看:53
本文介绍了javascript循环每9秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个html5应用程序,我希望在写入随机位置8秒后,如果你赢了或不赢一秒,它会写,然后它将从创建一个新的随机开始,我已经尝试过因为它似乎没有做任何事情,如果我写的话(真的)浏览器崩溃。

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.

有没有办法解决它?

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);
    }
});


推荐答案

您可以将setInterval用于此目的

You can use a setInterval for this purpose

参考

time=setInterval(function(){
//your code
},9000);

你可以清除这个

clearInterval(time);

这篇关于javascript循环每9秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆