PDA

View Full Version : Cannot stop running task when call Ext.TaskManager.stop() method!



chriswu00
18 Apr 2012, 8:27 AM
I set up a message box to display current time. The timer interval set as 1000, and there are 2 buttons to start and stop the 'timer'. When click on the 'stop' button it calls the Ext.TaskManager.stop method and update the text in the message box to 'paused'. However, when clicked the 'stop' button and the text changed to 'Paused' for just second, it changed back to display current time again. When i tried use Ext.TaskManager.stopAll(); instead of Ext.TaskManager.stop(task), it works! Why? My code as below:


<script type="text/javascript">


Ext.onReady (function(){

var config={


msg:'Display Time',
modal:true,
buttons:Ext.Msg.OKCANCEL,
fn:displayTime
}

Ext.MessageBox.msgButtons[0].setText('Start');
Ext.MessageBox.msgButtons[3].setText('Stop');

Ext.MessageBox.show(config);

function displayTime(id){

if(id=='ok'){

var task = {
run:function(){Ext.MessageBox.updateText ('????:' + Ext.util.Format.date(new Date(), 'Y-m-d g:1:s A'));},
interval:1000
}

Ext.TaskManager.start(task);
}

else {


Ext.MessageBox.updateText('Paused!');
Ext.TaskManager.stop(task);
}
};

});


</script>

scottmartin
19 Apr 2012, 9:14 AM
Please see the following changes:



var config = {


msg: 'Display Time',
modal: true,
buttons: Ext.Msg.OKCANCEL,
fn: displayTime
}

Ext.MessageBox.msgButtons[0].setText('Start');
Ext.MessageBox.msgButtons[3].setText('Stop');

Ext.MessageBox.show(config);

function start_task() {
Ext.MessageBox.updateText('????:' + Ext.util.Format.date(new Date(), 'Y-m-d g:1:s A'));
}

var task = { // moved outside function so it was not called over and over
run: start_task,
scope: this,
interval: 1000
}

function displayTime(id) {

if (id == 'ok') {
// task was here ... was not always defined due to if statement; was also recalled each time
Ext.TaskManager.start(task);
} else {
Ext.MessageBox.updateText('Paused!');
Ext.TaskManager.stop(task);
}
};


Regards,
Scott.