www.webdeveloper.com
Results 1 to 7 of 7

Thread: javascript loop every 9 seconds?

  1. #1
    Join Date
    Nov 2012
    Posts
    4

    Unhappy javascript loop every 9 seconds?

    Hallo,
    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?

    thank you,
    Boaz.


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

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    replace "8000);" with "i * 1000);"

  3. #3
    Join Date
    Nov 2012
    Posts
    4
    no it doesnt fix it because in the first time i is zero and then in the next time it doesnt loop
    is there an other way to fix it?

    thankyou,
    Boaz

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by bankas View Post
    no it doesnt fix it because in the first time i is zero and then in the next time it doesnt loop
    is there an other way to fix it?

    thankyou,
    Boaz
    if that's an issue, simply add one each time:
    "(i+1) * 1000);"

    looking a little closer, i now see you need to enclose the variables you are assigning to the html in a function, lest all the timeout function refer to the last iteration of the loop.

    for (var i=0;i<8;i++){

    could become

    for (var i=0;i<8;i++){ (function(){

    and your last "}" would then be "}()); }"

    that pattern should give you a private copy of the vars like Place that need to show up later as they were when dispatched.
    this is because the vars live in the inner function instead of the common outer one that the loop lives in...
    Last edited by rnd me; 11-27-2012 at 03:14 PM.

  5. #5
    Join Date
    Nov 2012
    Posts
    4
    now it doesnt act when i click, here the full code:

    Code:
    jQuery(document).ready(function(){
    
        for (var i=0;i<8;i++){ (function(){
            ImageClicked = false;
            var XYScore = 0;
            var RandomPlace=Math.floor((Math.random()*10)+1);
            var Place;
            var WantedXPr;
            var WantedYPr;
    
            switch(RandomPlace){... }
    t=true;
            setTimeout(function(){
                if (!ImageClicked){
                    $('#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)
    
    
    
    
    
            var RealWantedX = Scale * WantedXPr;
            var RealWantedY = HeaderHeight + Scale * WantedYPr;
    
    
    
                $("#special").click(function(e){
                    var TouchedX= e.pageX;
                    var TouchedY= e.pageY;
                    XDistance=RealWantedX-TouchedX;
                    YDistance=RealWantedY-TouchedY;
                    if(XDistance < 0){
                        XDistance *=-1
                    }
                    if(YDistance < 0){
                        YDistance *=-1
                    }
                    XDistance *=0.7;
                    YDistance *=0.7;
                    var XEntry = XDistance;
                    var YEntry = YDistance;
                    var XScore=50-XEntry;
                    var YScore=50-YEntry;
                    if(ImageClicked==false)
                    {
                    XYScore= XScore+YScore;
                    if  (XYScore < 0  ){
                     XYScore = 0}
                    XYScore = Math.round(XYScore);
                    Score +=XYScore;
                        ImageClicked=true;
                    }
                });
                    $('#x2').html(Score)
    
    
        }()); }
    });

  6. #6
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    broken in a new way isn't always bad; often its darkest just before it get's bright.

    doh! my bad; i forgot that i was outside, and that was the whole point of my extra closure.

    if you privatize it by modifying the wrapper it should work:


    for (var i=0;i<8;i++){ (function(i){

    // rest of the function body code here...

    }(i)); }

  7. #7
    Join Date
    Nov 2012
    Posts
    4
    no, it doesnt work, it just like before.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles