I've got this Ajax call that basically populates a dropdown box dynamically, however occasionally it seems to get stuck and just sits there doing nothing, if I reprompt it by changing the dropdown before it, it then works.

Somehow I need to add a timeout and retry into the code chunk below - any ideas how?

function getTownsResorts(sel)
{
	var TheProvinceID = sel.options[sel.selectedIndex].value;
	document.getElementById('UrbanizationID').options.length = 0;	// Empty city select box
	if (TheProvinceID.length > 0) {
		var index = ajax.length;
		ajax[index] = new sack();

	ajax[index].requestFile = 'scripts/ajax/ajax_GetUrbanizations.php?ProvinceID=' + TheProvinceID;	// Specifying which file to get
	ajax[index].onCompletion = function(){ createTownsResorts(index) };	// Specify function that will be executed after file has been found
	ajax[index].runAJAX();		// Execute AJAX function
}
}

    You should have posted this in the clientside forum. Try googling javascript setTimeout. You'll find pages like this.

      Many thanks mate, thats brilliant and has helped me massively.

        Actually, in this particular instance it appears that the Ajax code already has an 'onCompletion' statement, so would I still need to add such a Timeout call?

        ajax[index].requestFile = 'scripts/ajax/ajax_GetProvinces.php?RegionID='+TheRegionID;
        ajax[index].onCompletion = function(){ createProvinces(index) };
        ajax[index].runAJAX();

        So far I've tried wrapping this element with a setTimeout...

        ajax[index].runAJAX();

        as this...

        setTimeout("ajax[index].runAJAX()",1500);

        But I get the error...

        How do I properly encase this string into a setTimeout?

          The tricky part with this whole thing is that it is asynchronous. I'm not entirely sure if an AJAX call is 100% reliable or not OR whether the onCompletion call even knows anything about which object in your javascript called it in the first place.

          I've used the Yahoo User Interface (YUI) tools and their AJAX object has some kind of timeout parameter as I recall.

          In your case, the var index is defined inside a function so only code within that function will have visibility to it. You might consider defining the var index OUTSIDE the function that gets your towns. On the other hand, you might end up creating a number of requests to the same page, all of which return and execute. I think the YUI ajax object knows how to properly cancel a request that has timed out.

          Sorry I can't be more help.

            Write a Reply...