At work recently I came across an instance where I wanted to turn a script.aculo.us Ajax.Periodical Updater on and off.

After looking on the wiki it appeared that there was no way to turn the thing off once you had started it other than reloading the page. You could for example want to turn it off after an hour or when a user clicks a button.

So I came up with this code which uses a normal updater which on complete waits for a set amount of time before calling another function. This function checks a global variable to see if the updater is on or off and depending on the outcome either updates again or does nothing (stops it).

The code is below and also includes some Hijax to convert a normal link into our toggle button.


var global_updater_status = 'on';

function apply_toggle_updater() {
	// In your HTML you need a button or anchor like; Toggle Updater with the id relating to below
	// You also need to run this function onload
	if( document.getElementById('toggle_updater') ){
		var theButton = document.getElementById('toggle_updater');
		theButton.onclick = 	function(){
										toggle_updater(); return false;
									};
	}
}

function toggle_updater(){
	if(global_updater_status == 'on'){
		global_updater_status = 'off';
	}else{
		global_updater_status = 'on';
		Updater();
	}
}

function Updater() {
	var url = 'ajaxGetStatus.php';
	var pars = "id=8";
	var target = "status";
	var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars, asynchronous:true, evalScripts:true, onComplete:function() { setTimeout('Periodical()',2000); } });
	// This one updates every 2 seconds as long as the global variable is set to on
}

function Periodical(){
	if(global_updater_status == 'on'){
		Updater();
	}
}

Hope this helps!

This entry was posted on Thursday, September 28th, 2006 at 3:22 pm and is filed under Ajax, Development, Hijax, JavaScript. You can leave a comment and follow any responses to this entry through the RSS 2.0 feed.

4 Comments Leave a comment

  1. jocko jones 18 August 2007 at 4:06 pm #

    hi, thanks for the code… as a hobbyist coder i wasn’t sure what to do with ‘myAjax’ to have it execute, so i altered it and got it to work.

    the question i have now is if there is any way to have the div refresh at an interval of 1 as opposed to 2?

    i tried using Ajax.PeriodicalUpdater, but it seems to only start the process and not stop.

  2. jocko jones 18 August 2007 at 6:51 pm #

    i figured it out:

    setTimeout(‘Periodical()’,2000)

    change to:

    setTimeout(‘Periodical()’,1000)

    on a sidenote i would suggest changing:

    var global_updater_status = ‘on’;

    to

    var global_updater_status = ‘off’;

    generates more natural behaviour :)

  3. Darko 22 August 2007 at 12:30 pm #

    To turn a Ajax.Periodical Updater off you have to set its onComplete to undefined then call stop();

    Hope that helps…

    Dk0

  4. PukSeld 3 May 2010 at 3:04 pm #

    NiksWart say: I think, that you commit an error. I can defend the position. Write to me in PM, we will discuss.

    _____________
    livitra
    for
    9

Leave a Reply