Hi! im trying to make a simple counter with the function setInterval. I can't get to figure it out how the seconds and the minutes could work together. The first 60 seconds the minute counter skips, it just stays on 0 until the next round 60+58 seconds it counts up to 1. Here is the code im trying to work with
var valuemin = 0; var value = 0; starta.addEventListener('click',function(){ setInterval(function(){ label.text = value++; if(value == 61){ value = 1;} },1000); setInterval(function(){ label3.text = valuemin++; },60000); });If instead skip the value / valuemin, and just use label.text++; and label3.text++; The counter works in the simulator, but not on the phone. This option doesnt sync them in the phone. Anyone have a solution or a suggestion of how i should do instead to get a simple counter working on the phone?
2 Answers
Javascript timing functions are notoriously inaccurate. The longer those timing functions run, the farther off they will get from true time. It would be much easier to grab the phone time at certain intervals and compare it with the previous time. The difference between the two times is the amount of time that has passed. You can easily use a single label for that difference as well instead of trying to keep track of minutes and seconds.
If it's for ios you could use this module:
https://marketplace.appcelerator.com/apps/2658?1998947474
Your Answer
Think you can help? Login to answer this question!