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.
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);
}
});
We can create an infinite loop using while statement. If the condition of while loop is always True , we get an infinite loop.
The standard way of creating a delay in JavaScript is to use its setTimeout method. For example: console. log("Hello"); setTimeout(() => { console.
To use Javascript promises in a for loop, use async / await . This waits for each promiseAction to complete before continuing to the next iteration in the loop.
Answer: Use the JavaScript setInterval() method You can use the JavaScript setInterval() method to execute a function repeatedly after a certain time period. The setInterval() method requires two parameters first one is typically a function or an expression and the other is time delay in milliseconds.
You can use a setInterval for this purpose
Reference
time=setInterval(function(){
//your code
},9000);
you can clear this by
clearInterval(time);
This is more elegant.
(function loop() {
setTimeout(function () {
// execute script
loop()
}, 9000);
}());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With