www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Loop issue

  1. #1
    Join Date
    Mar 2013
    Posts
    61

    resolved [RESOLVED] Loop issue

    I have developed a very simple game with my own game loop on a timer.

    My issue is how do I stop this timer when the game has been won by one of two player?

    Right now I have an alert that fires repeatedly once the game has finished.

    Code:
    function playerWin()
    {
    	switch (pingpong.scoreA || pingpong.scoreB){
    		
    		case 10:
    				alert("One of you lose! " + this.toString());
                                     pingpong.timer = 0;
    			break;
    		
    	}
    }

  2. #2
    Join Date
    May 2014
    Posts
    649
    Did you store the interval/timer's handler to a variable so you can clear it? I'm assuming you used setInterval?

    If so, you want to use clearInterval:
    https://developer.mozilla.org/en-US/....clearInterval

    Really though, without seeing how you are setting the timer and responding to it, it's hard to say more.
    Java is to JavaScript as Ham is to Hamburger.

  3. #3
    Join Date
    Mar 2013
    Posts
    61
    Yep.

    pingpong.timer = setInterval(gameloop,30);

  4. #4
    Join Date
    May 2014
    Posts
    649
    Ok, so instead of

    pingpong.timer = 0;

    You should have:

    clearInterval(pingpong.timer);

    The value returned is NOT how long is left, it's basically a pointer to the timer's data structure; hence why you have to clear it with the function.

    I'll often set it to null or false AFTER clearInterval, so I can test if it's active or not without an extra variable.
    Java is to JavaScript as Ham is to Hamburger.

  5. #5
    Join Date
    Mar 2013
    Posts
    61
    Quote Originally Posted by deathshadow View Post
    Ok, so instead of

    pingpong.timer = 0;

    You should have:

    clearInterval(pingpong.timer);

    The value returned is NOT how long is left, it's basically a pointer to the timer's data structure; hence why you have to clear it with the function.

    I'll often set it to null or false AFTER clearInterval, so I can test if it's active or not without an extra variable.

    Worked great. Thank you very much.

Thread Information

Users Browsing this Thread

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

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