In my application there are some procedures requiring a lot of time, so I need to display progress step by step without getting timeout.
This is the controller "procedura"; the user starts the execution with the action "esegui":
public function actionEsegui($id) {
$procedura = $this->loadModel($id);
$this->render('esecuzione', array(
'model' => $procedura,
));
}
public function actionEsecuzione($id) {
$procedura = $this->loadModel($id);
$procedura->eseguiProcedura(); // <--- this starts the execution
}
public function actionAvanzamento() {
echo '...';
}
public function actionRisultato($id) {
$procedura = $this->loadModel($id);
$this->render('risultato', array(
'model' => $procedura,
));
}
The view "esecuzione" display a simple page including three tasks:
-
start the real procedure execution thru action "esecuzione"
-
refresh the processing status with output of action "avanzamento"
-
redirect to "risultato" at end of execution
...
echo CHtml::openTag('div', array('id' => 'contenuto'));
echo 'Elaborazione in corso...';
echo CHtml::closeTag('div');
Yii::app()->clientScript->registerScript('avvio' , "
setInterval(function() {
jQuery.ajax({
type: 'POST',
dataType: 'html',
url: '" . Yii::app()->createUrl('procedura/avanzamento') . "',
success: function(data){
document.getElementById('contenuto').innerHTML = data;
},
error: function(){
document.getElementById('contenuto').innerHTML = 'Errore di esecuzione';
}
});
}, 1000);
jQuery.ajax({
type: 'POST',
dataType: 'html',
url: '" . Yii::app()->createUrl('procedura/esecuzione', array('id' => $model->id)) . "',
success: function(data){
window.location.assign('" . Yii::app()->createUrl('procedura/risultato', array('id' => $model->id)) . "');
},
error: function(){
document.getElementById('contenuto').innerHTML = 'Procedura interrotta';
}
});
", CClientScript::POS_LOAD);
The procedure starts correctly and, at the end, redirect to “risultato”. But the processing status is not refreshed. Someone can help to understand what’s wrong?
Thanks