cruzlutor Posted February 3, 2017 Share Posted February 3, 2017 Hello guys I'm working on a javascript game, some times I need to call a function 2, 3 or X seconds after an action, the question is. What is better in performance, create multiple setTimeout() or use the main loop to evaluate a function queue? thanks Quote Link to comment Share on other sites More sharing options...
mattstyles Posted February 3, 2017 Share Posted February 3, 2017 What do you think 'use the main loop to evaluate a function queue' means? Quote Link to comment Share on other sites More sharing options...
cruzlutor Posted February 3, 2017 Author Share Posted February 3, 2017 With time tick on main loop init(){ this.attackTimer = game.time } loop(){ // attack 1 time each second if(game.time > this.attackTimer + 1000){ this.attack() this.attackTimer = game.time } } But you can have a array of functions to evaluate Quote Link to comment Share on other sites More sharing options...
mattstyles Posted February 3, 2017 Share Posted February 3, 2017 And loop is presumably tied to requestAnimationFrame (raf) or setTimeout? If it is tied to raf and you're supporting older browsers then you'll probably have a fallback in there to use setTimeout (or even setInterval, but thats a far worse solution). The simple answer is, no performance difference. It is likely that adding it to main loop is slight less performant as it has to do a conditional, but this is so ridiculously cheap as to make no matter. The question for you really is not one of performance, but what is easier for you (as dev) to manage. If you are calling functions that aren't really part of the lifecycle of your app/game, such as delaying an ajax request (for example), then setTimeout probably makes more sense, but if you're calling functions that need to be called regularly, maybe the main loop makes more sense. By shoving them all in the main loop they are all in one place, which from a management point of view could be good, or, it might get two noisy in there, so it could be bad. I guess the point is that there is no hard-and-fast rule and you have to weigh up the pros and cons for your situation and choose appropriately, safe in the knowledge that you can probably change this sort of thing fairly easier further down the line (something to watch out for is which functions you are calling i.e. are they available everywhere to call? is the data they need to work with also available? these are architectural issues and could have issues if you want to change later). cruzlutor 1 Quote Link to comment Share on other sites More sharing options...
cruzlutor Posted February 3, 2017 Author Share Posted February 3, 2017 Just now, mattstyles said: If you are calling functions that aren't really part of the lifecycle of your app/game, such as delaying an ajax request (for example), then setTimeout probably makes more sense, but if you're calling functions that need to be called regularly, maybe the main loop makes more sense. By shoving them all in the main loop they are all in one place, which from a management point of view could be good, or, it might get two noisy in there, so it could be bad. That is a good point : ), and the main loop solution is easier to manage for me. thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.