I'm building a game that has a progress bar for the remaining time, but when user do a action, the progress bar does not remain stable at the same speed.

This action uses much of Math.floor((Math.random() * limit):

You can check my code in GitHub and see it running and tests passing (except that I described above): https://github.com/acazsouza/twinoo

How can I improve that code?

(Although I'm passing the link to the entire repository, the code is small)

share|improve this question
It seems that it's not your code, but the jQuery progress bar plugin you are using is that progressing that bar using the velocity option. Unless you want to rewrite plugin, you should change that you change the value of the progress bar instead. – RoToRa Apr 26 '12 at 9:20
2  
-1 Code Review questions are supposed to contain the actual code you want reviewed. – Ross Patterson Apr 26 '12 at 10:57
@RossPatterson Hi, look the code again please... I write the ProgressBar plugin, but this not do anything anormal, only use jquery animate function in that time: (10000 / speedVariable). – Acaz Souza Apr 26 '12 at 18:48
2  
@AcazSouza: The point is, you're supposed to include the code in the question. StackExchange sites are intended to have long-term value, and not just link to things. – Ross Patterson Apr 26 '12 at 21:23

closed as off topic by Winston Ewert Apr 29 '12 at 14:55

Questions on Code Review - Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

If your game is running in a browser, that can get very problematic. JavaScript in browsers does not support true concurrency, so if a function is running (any function, from any source - setTimeout, event handlers, ajax callback, newly loaded scripts, etc) others can't run until that function execution ends. I don't know for sure how much it applies to your case, but as a general rule any "logic" code that takes a noticeable time to run will interfere with your "view" code.

If I recall correctly, there was a work in implementing threads in browser (JavaScript Worker Threads) as part of the HTML5 standard. Dunno the current state of support for that, but I'd say it's your best bet at solving this issue (short of using browser plugins).

share|improve this answer

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