prototypejs / prototype

Prototype JavaScript framework
http://prototypejs.org/
Other
3.54k stars 639 forks source link

Documentation not clear about how to stop periodical updater after specification #284

Closed kressly closed 9 years ago

kressly commented 9 years ago

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.

fntz commented 9 years ago

Use:

var pu = new Ajax.PeriodicalUpdater('container', '/url', {
  decay: 2,
  onSuccess: function(r) { //or onComplete
     if (pu.decay > 8) {
       pu.stop();
     }
   }
});
kressly commented 9 years ago

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

fntz commented 9 years ago

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

kressly commented 9 years ago

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 ?

fntz commented 9 years ago

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);
fntz commented 9 years ago

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
          }
        }
} 
kressly commented 9 years ago

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

fntz commented 9 years ago

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.

kressly commented 9 years ago

Thanks a lot