Reseting timer when timer is on 0: 00 in Android

You must Login before you can answer or comment on any questions.

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
});

— asked 7 months ago by Gus Johansson
1 Comment
  • If you are getting an application crash, it really is necessary to post the crashlog and any console output that may be relevant to your crash. Also, you've said that you are using Android, but you have provided no other information regarding your environment, when all of that data is very relevant to a crashing application. If you have any questions regarding what environment information is necessary, please see the Ask a Question Page

    — commented 7 months ago by Anthony Decena

Your Answer

Think you can help? Login to answer this question!