There was a completely unnecessary Math.round in the else block.
var myTimer = new Timer( 5000, onUpdate, 100);
myTimer.start(); //begins the execution of the timer
myTimer.pause(); //pause the timer
myTimer.start(); //restart the timer where it left
myTimer.reset(); //pause the timer and reset into it's original time
myTimer.reset( 5000 ); //resets the timer to 5000 milliseconds
//Called when application is started.function OnStart(){ //Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
timer = new Timer(7000,function( time ){ txt.SetText("Time: " + (time/1000).toFixed(2)); },1000/20);
//Create a text label and add it to layout. txt = app.CreateText( "Hello" );
txt.SetTextSize( 32 ); lay.AddChild( txt );
btn = app.CreateButton( "Start Timer" ); btn.SetOnTouch( startTimer ); lay.AddChild( btn ); btn = app.CreateButton( "Pause Timer" ); btn.SetOnTouch( pauseTimer ); lay.AddChild( btn ); btn = app.CreateButton( "Reset Timer" ); btn.SetOnTouch( resetTimer ); lay.AddChild( btn ); btn = app.CreateButton( "Calc Something Big" ); btn.SetOnTouch( bigCalc ); lay.AddChild( btn );
//Add layout to app. app.AddLayout( lay );}
function bigCalc(){ for( var i = 0; i < 10000000; i++){ Math.random()*Math.random(); } }
function startTimer(){ timer.start();}
function pauseTimer(){ timer.pause();}
function resetTimer(){ timer.reset();}
//timer objectfunction Timer(time, updateCallback, updateInterval){ //recieve time in milliseconds, updateCallback updateInterval in milliseconds var self = this; this.running = false; this.lastTime = false; this.time = time || 10000; this.originalTime = this.time; this.updateCallback = updateCallback || function(){}; this.updateInterval = updateInterval || 500; this.start = function (){ if (self.running) return; self.running = true; app.ShowDebug("Timer running!: " + self.time); self.lastTime = Date.now(); setTimeout(self.count(),0); }; this.pause = function(){ self.count(); self.running = false; }; this.reset = function( time ){ self.running = false; self.time = time || self.originalTime; self.updateCallback( self.time ); }; this.count = function (){ //countdown by the time that passed //calc how much time passed since last execution if (!self.running) return; var newTime = Date.now(); var timePassed = newTime - self.lastTime; self.lastTime = newTime; //update time counter self.time -= timePassed; if ( self.time <= 0 ){ self.time = 0; self.updateCallback( 0 ); self.running = false; return; } self.updateCallback( self.time ); setTimeout(self.count, self.updateInterval); };}
timer = new Timer(7000,timer_OnCallBack,1000/20);
function timer_OnCallBack( time ){ txt.SetText("Time: " + (time/1000).toFixed(2)); }
Not only have you used the ideal way to update your screen without danger of blocking your code
you have also produced a very nice sample object that would make an excellent teaching aid.
One thing I learned was that when I created multiple buttons on a layout and did not need to refer to them again, I do not need to push the original btn references into an array