What is the correct way to reschedule?

Hi, i want to do a reschedule, what is the best way?

var timeRepeat = 5;

init : function (){
this.schedule(this.repeatAgain, timeRepeat);// <---- this option?
this.scheduleOnce(this.repeatAgain, timeRepeat);// <---- this option?
},

repeatAgain : function (){
cc.log(“repeat every:”+ timeRepeat +" seconds"); // 5, 4, 3 …
timeRepeat–;

this.unschedule(this.repeatAgain);// <---- this option previously its necesary?
this.schedule(this.repeatAgain, timeRepeat);// <---- this option?
this.scheduleOnce(this.repeatAgain, timeRepeat); <---- this option (make troubles)?
},

I assume that you want repeatAgain will be executed timeRepeat times.

There are some errors in your code. The second argument of schedule is interval in seconds, if you want to use schedule, the code is refactored as:

var timeRepeat = 5;

init : function (){
    this.schedule(this.repeatAgain, timeRepeat, 0, timeRepeat);// schedule after timeRepeat seconds
},

repeatAgain : function (){
    cc.log("repeat every:"+ timeRepeat +" seconds"); // 5, 4, 3 .....
    timeRepeat--;
    if (timeRepeat > 0) { //It's mandatory, or dead loop
        this.schedule(this.repeatAgain, timeRepeat, 0, timeRepeat);
    }
},

The second argument of scheduleOnce is the amount of time that the first tick will wait before execution, if use scheduleOnce, the code ie refactored as:

var timeRepeat = 5;

init : function (){
    this.scheduleOnce(this.repeatAgain, timeRepeat);
},

repeatAgain : function (){
    cc.log("repeat every:"+ timeRepeat +" seconds"); // 5, 4, 3 .....
    timeRepeat--;
    if (timeRepeat > 0) {//It's mandatory, or dead loop
        this.scheduleOnce(this.repeatAgain, timeRepeat); 
    }
},

Hope this can help.

1 Like

Thanks for the answer, I don´t know why when reschedule “scheduleOnce” have troubles, currently I use with good results:

    //global variable
    timeRepeat = 5;//this is a random number between 2 and 5
    
    init : function (){
        this.repeatAgain();
    },
    
    repeatAgain : function (){
        this.schedule(this.thingsToDo, timeRepeat );                 
    },

  thingsToDo : function(){
       cc.log("repeat every:"+ timeRepeat +" seconds"); // 5, 4, 3 .....
       timeRepeat = 3; //this is a random number between 2 and 5
       this.unschedule(this.repeatAgain);
       this.scheduleGamePlaySpeed(); 
},