Hey everybody, I have a function called "countDown" which simply creates a timer counting down, on button press Id like the timer to start (the timer lasts for 60 seconds) and then after 61 seconds I would like it to reset by itself.
CODE:
var win = Ti.UI.createWindow({ backgroundImage:'background.png.jpg', exitOnClose:true, }); win.open(); var buttonWolf1 = Ti.UI.createButton({ backgroundImage:'120px-WolfSquare.png', id:"Wolf1", width:100, height:100, top:30, left:140, }); var displayWolf1 = Titanium.UI.createLabel({ text:"1 : 00", height:40, width:160, top:50, left:0, color:'#fff', font:{ fontSize:35, fontWeight:'bold' }, textAlign:'center' }); win.add(buttonWolf1); buttonWolf1.addEventListener('click',function(){ my_timer4.start(); // Start the timer win.add(displayWolf1); // Add the label next to the timer setTimeout (resettimer1(), 61000 ); // After 61 seconds reset timer...? NOT WORKING }); var resettimer1 = function() { my_timer4.stop // Stop the timer my_timer4.set // Reset the timer } // Global countdown function var countDown = function( m , s, fn_tick, fn_end ) { return { total_sec:m*60+s, timer:this.timer, set: function(m,s) { this.total_sec = parseInt(m)*60+parseInt(s); this.time = {m:m,s:s}; return this; }, start: function() { var self = this; this.timer = setInterval( function() { if (self.total_sec) { self.total_sec--; self.time = { m : parseInt(self.total_sec/60), s: (self.total_sec%60) }; fn_tick(); } else { self.stop(); fn_end(); } }, 1000 ); return this; }, stop: function() { clearInterval(this.timer); this.time = {m:0,s:0}; this.total_sec = 0; return this; } }; }; // Timer for Wolf1 var my_timer4 = new countDown(1,00, function() { displayWolf1.text = my_timer4.time.m+" : "+my_timer4.time.s; }, function() { sound.play(); });
At the moment the application crashes using android device. I appreciate any help a lot! I have tried:
while(true) { if (displayWolf1.text > 0 : 1 || displayWolf1.text == 0 : 0) { mytimer_4.set }AND
buttonWolf1.addEventListener('click',function(){ my_timer4.start(); // Start the timer win.add(displayWolf1); // Add the label next to the timer setTimeout (my_timer4.set, 61000 ); // After 61 seconds reset timer...? NOT WORKING EITHER });
Your Answer
Think you can help? Login to answer this question!