Well, I've been burning the midnight oil trying to figure out how to issue the stop() command to a PeriodicUpdater. I've come up with a solution that uses setTimeouts to call the stop function. I think my solution is very kludgey... especially since I'm new to Javascript + Prototype.

Can anyone let me know if the below code is at all proper? :)

---------
Summary: A PeriodicUpdater calls a server script which returns the percent finished of a process as a JSON encoded string in a x-json header. When the percent is > 10, I'd like to STOP the periodicupdater from making any further AJAX calls.

The problem: If I issue pb.stop() in the onSuccess function [pbCheck()], nothing happens. The script continues!

The workaround: Set percent value using pbCheck, Use setTimeout to continually poll pbKill() which checks the percent value & if it's greater than 10, issues STOP to updater before destroying it (when a STOP is recieved, the onComplete function is called[pbClean()]).

<script type="text/javascript">
// <![CDATA[

if (!pb) {
var pb =
new Ajax.PeriodicalUpdater("ajaxUpdate", "ajax_mailingStatus.php", {
asynchronous:true,
frequency : 2,
onSuccess: pbCheck,
onComplete: pbClean
});
}

var percent = 0;
function pbCheck(request,json) {
   percent = json.percent;
}

setTimeout(pbKill,5000);
function pbKill() {
 if (percent > 10) {
     pb.stop();
 }
 else {
     setTimeout(pbKill,5000);
 }
}

function pbClean() {
 pb = null;
}
}
// ]]>
</script>
------

Thanks,

~ Brice Burgess
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

Reply via email to