Closed kressly closed 9 years ago
Use:
var pu = new Ajax.PeriodicalUpdater('container', '/url', {
decay: 2,
onSuccess: function(r) { //or onComplete
if (pu.decay > 8) {
pu.stop();
}
}
});
Thanks a lot Now i tried to modify my original script but it is not working. Please can you tell me how to go with this
function mettreajour_periodique(span_id, url_traitement, nos_parametres, our_frequency, our_decay)
{
var ajax = new Ajax.PeriodicalUpdater ({success: span_id}, url_traitement, {method:'get', frequency: our_frequency, decay: our_decay, parameters: nos_parametres, evalScripts: true, onSuccess: function(r) { //or onComplete
if (ajax.decay > 8) {
alert('Yes');
ajax.stop();
}
}
});
}
//We apply the function here
mettreajour_periodique('nombre_nouveaux_messages', 'barre_du_haut_actualise.php', '&membre=<?php echo $membre; ?>', 5, 2);
I was expecting it to give me an alert when it is above 8 but it took a long time before giving me the alert. Is there something i am doing wrong ?
Thanks
Your code is correct.
Next iteration for update calculate as: decay * frequency
,
the initial decay is 2, and frequency is 5. When you content not changes, then decay is 2, 4, 8, 16. and time for next request is 10, 20, 40, 80 seconds, see here https://github.com/sstephenson/prototype/blob/d9411e5/src/prototype/ajax/periodical_updater.js#L254
Thanks a lot. Now what i really want is that as the next request reaches 20 for example i want it to stop and restart from the initial as in 5 for frequency and 2 for decay. How can i achieve that ?
Try:
function mettreajour_periodique(span_id, url_traitement, nos_parametres, our_frequency, our_decay)
{
var ajax = new Ajax.PeriodicalUpdater ({success: span_id}, url_traitement, {method:'get', frequency: our_frequency, decay: our_decay, parameters: nos_parametres, evalScripts: true, onSuccess: function(r) { //or onComplete
if (ajax.decay > 8) {
alert('Yes');
ajax.stop();
//call again
mettreajour_periodique('nombre_nouveaux_messages', 'barre_du_haut_actualise.php', '&membre=<?php echo $membre; ?>', 5, 2);
}
}
});
}
//We apply the function here
mettreajour_periodique('nombre_nouveaux_messages', 'barre_du_haut_actualise.php', '&membre=<?php echo $membre; ?>', 5, 2);
also, my code possible raise StackOverflow error, for more comfortable usage, use some like this
var iter = 0;
function mettreajour_periodique(...) {
// check iter variable
if (iter > 10) {
return;
}
var ajax = new ...
onSuccess: function() {
if (ajax.decay > 8) {
ajax.stop();
iter ++; //increment
mettreajour_periodique( //call again
}
}
}
Thanks a lot but i did not get the second code well Where exactly will the var iter be placed in the function ? Am i supposed to add it to the parameters of the function mettreajour_periodique ? Otherwise how can it be used in the function ? Praticaly with my previous code what am i going to get ? And really thanks for you help
Where exactly will the var iter be placed in the function ?
in any place of mettreajour_periodique function
Am i supposed to add it to the parameters of the function mettreajour_periodique
yes. it's good idea, you might pass it as parameter, and then increment.
Thanks a lot
Hello Please how exactly to stop the periodical updater if the frequency reaches a certain number because of the decay ? I have read the documentation but could not figure it out.