Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am setting up a JavaScript Server for my Game.

Am I understanding this correctly:

If I use setInterval to call a function every second, and takes 2 seconds to process. Then I am going to "stack up" requests indefinetly the Client will become more and more out of sync?

If I use setTimeout, and specify 1 second. Then the function will run (again, lets say 2 seconds) and then start the timeout. And not stack up requests.

Edit: I am doing Pathfinding and sending it to the Client. Thank you for all the help people. I think using the Callback is the best option for this.

share|improve this question
    
Well you can measure how long did it take to finish and then dont call other function in 1 sec, but 1 sec - time that it take to finish. But in your example where it takes 2 sec function that should take only 1 nothing will work - but two seconds is really long time (for a game logic). –  Kikaimaru Oct 29 '12 at 9:54
add comment

2 Answers 2

up vote 4 down vote accepted

What you are trying to do sounds like a typical case for callback functions instead of SetTimeout/Interval. Especially with network communication (Longpolling, etc.) SetInterval is a bad choice.

Instead of using setTimeout or setInterval in most cases it's more appropriate to make use of javascripts asynchronical nature and use callbacks.

This means that you hand over a function that is executed as soon as the first function has finished.

Here is a brief explanation on how to use callbacks. Or you can read several question about that topic on Stackoveflow (e.g. here).

share|improve this answer
    
+1, Good point. –  Nick Wiggill Oct 29 '12 at 11:29
    
The whole idea for my server is to run as fast as possible, but not lose data of course. This is exactly what I needed. Thank you :) –  Oliver Schöning Oct 29 '12 at 11:45
add comment

When using setInterval, you have as much time as you've specified for the interval parameter, to do the work. It's not a "blocking" wait, the timer runs in parallel with your code. So just set it to something safe, like 2 or 3 seconds. That's your period in which you can get things done. This is how all JS games work. You should be able to test this easily in the browser, just set up a small program that adds a million numbers in place of your 2-second task, have it run every n seconds, and see how that affects setInterval's firing. EDIT I think what you'll find (if you write a very simple test) is that if you have something that is blocking for 2 seconds, but your interval is set to 1 second, then yes, as soon as your first function call completes it's going to jump straight into the second one (because it will have already backed up), but the timer will fall further and further behind. In any real time application, you have to be able to guarantee that work is going to be completed within some maximum time period. This is not an option. For more information, read up on Gaffer's Fix Your Timestep which discusses issues of timing in detail.

It shouldn't be taking you that long to process anything in a game (client or server). If it is, then your code is either highly inefficient or it needs to be broken up into individual "tasks" with higher granularity, which can then be fed into the function. That way, you'd have no problems with setInterval. Just a thought.

Just to add... It seems to me that if you tell us what you are trying to achieve, we can help you find the best solution. Right now, I'm feeling that what you're attempting is missing the mark.

share|improve this answer
    
Does this mean that if setInterval is 1 second. And the function needs 2 seconds. That the function will be executed in two setInterval runs? Or is the function simply not run if the time runs out? –  Oliver Schöning Oct 29 '12 at 10:22
1  
@OliverSchöning See edit. –  Nick Wiggill Oct 29 '12 at 10:26
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.