Problem with setTimeout and function returns
Hi all
Does anyone know of a sensible way of making setTimeout or setInterval (or any similar function) pause the execution of the calling script until they return?
For compatibility with a legacy system, I need to make a function call return a value from a server-side script.
To accomplish this, Iâve presently got the script loading the request data into a form in a child frame, which is submitted to the server-side script for processing. The server then returns a new form containing the results of the processing (so itâs not instantly available to the script â I have to handle the delay and only return when the data has appeared).
The problem here is that the one part of this design thatâs absolutely critical is that the data comes back in a function call. If I wanted it to come back in an alert() or document.write(), I could (and have) done it. You make the return function call itself within a timeout and, when it finds data, output it.
When I try this within returning the data as a function return, the basic problem seems to be that both setTimeout and setInterval mark the code to run in the future and then keep going. This means that the code runs the first time, finds the script hasnât yet returned so sets itself to run again in the future and then keeps going. Net result it runs once, finds itâs not there and returns an error saying it canât find the data. Whereas what Iâd much rather it did would be pause execution of the script until the timeout had been met.
Iâm aware I could theoretically do this by executing a do-nothing loop that sat there until the function returned â but that would be a real processor hog.
Iâm also aware I could do this as AJAX â but Iâm not over-familiar with it and neither are others whoâd have to maintain this script, so Iâm trying to avoid that for simplicity at this stage.
Does anyone know a (non-CPU hogging) way of making either setTimeout or setInterval actually stop execution of whatever has called them until their condition has been met? Or an alternative means of polling an external variable on a schedule until it contains a specified item or a timeout is reached?
Thanks,
Greg
|