Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Currently i develop (php,mysql) turn based strategy game and i need to put time limits. Never used javascript before. My question is how to make function for the following thing :

Player 1 wants to attack player 2. Player 1 press the attack button. The page is showing Time left untill battle (example : 05:00 minutes + javascript cooldown) After the time is 00:00 the script is executed.

Thanks in advance. i don't have an idea how to start so i would like to get any suggestions or examples from where to start.

share|improve this question
    
Don't do this in JavaScript. Client side scripts are easily modified, so your game is prone to cheating. –  Gerald Schneider Jun 25 at 19:36
    
Thanks for the tip. So left the question how to make realtime cooldown timer to show the players the time left. –  NazGul Jun 25 at 19:41
add comment

1 Answer

up vote 0 down vote accepted

You need to use the interval and timeout functions from the window object.

I suppose that this could do the trick:

(function (){
    var i = window.setInterval(update_timer, 1000);
    var t = window.setTimeout(function(){
        window.clearInterval(i);
        update_server();
    });
    set_timeout_canceller(function(){
        window.cancelTimeout(t);
    });
})(); 

Where:

  • update_timer must take care of displaying the time left,

  • update_server will send the necessary data at the end of the timeout.

  • set_timeout_canceller set the function to be called to cancel the timeout if player b made his move before the time limit.

You'll certainly have to refactor that code into your own, but I suppose that you get the idea.

Alternatively, you could have an ajax round trip at each interval to request the time left, and use that information only to decide when to timeout (thus leaving the timeout functions out of the equation).

share|improve this answer
    
Thanks. i've managed how to do it. everything is php based. javascript is used only to show the time left. –  NazGul Jun 25 at 20:56
    
Ok, glad you got it to work the way you wanted. Hopefully next time I'll be more useful :) –  didierc Jun 25 at 21:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.