View Full Version : Is it possible to use setInterval to return a function???????
mtenpow
01-06-2003, 05:59 PM
Is there a way to have a function return a value only after a specific event has been invoked or certain conditions have been met elsewhere in script?
I've tried the following:
this_func=arguments.callee;
interval=null;
interval=setInterval("if(__EventHasBeenInvoked()){ clearInterval(interval);this_func.external_return(svc);}",500);
Function.prototype.external_return=function (ret) {
this.return(ret);
}
It clearly did not work. I know that it's possible to implement a loop that breaks when my conditions have been met, but for operations that require a long time I think that this strategy would be ridiculously expensive.
Thank you for your time.
ez4me2c3d
01-06-2003, 06:06 PM
you could simply run a while loop and have it checking for a variable that gets created at the very end of your function... for example..var done = 0;
function doIt() {
//code goes in here
//and the last line should read something like
done = 1;
}
//now here is your loop to check and see if the function has finished
while (!done) {
//code goes here
}
i assume you would either call the function from an onLoad or from within the loop. I had a hard time understanding what exactly you were asking for, so this may not be the answer you need. my apologies.
glenngv
01-07-2003, 07:40 AM
when using setInterval or setTimeout and you call a function with parameters, you need to concat the parameter like this:
if the parameter is an integer:
interval=setInterval("if(__EventHasBeenInvoked()){ clearInterval(interval);this_func.external_return("+svc+");}",500);
if the parameter is a string:
interval=setInterval("if(__EventHasBeenInvoked()){ clearInterval(interval);this_func.external_return('"+svc+"');}",500);
mtenpow
01-07-2003, 07:24 PM
Here's a very simple description of what I was trying to do:
function obj(){
this.prop=false;
}
obj.prototype.onimport=function(){
this.prop=true;
}
function foo(){
v=new obj();
//wait for v.prop to be true and return ONLY THEN
}
Yes, I understand that a loop would work in this situation but wouldn't that needlessly burn a lot of CPU cycles? I'd MUCH rather check the values every 500 milliseconds as v.onimport() may sometimes be called after 3 seconds.
In the real code, v.onimport is the v's event handler for its "import" event. This event is only fired after v makes an asynchronous call to the server and retreives information. The amount of time it takes for this to occur obviously varies.
It IS possible to make the call synchronously, thereby halting all threads while this is processing but only, as far as i know, with XMLHttpRequest in recent IE and Mozilla builds. In order to have this functionality downgrade to, I use iframe remote scripting when XMLHttpRequest fails and I can think of no way to make this method synchronous.
An alternative to all of this is simply to have function foo return obj v regardless of whether or not the RPC has been completed. Other code that uses function foo must therefore only use it after the event has been fired. This is much less elegant but it is an option in lieu of all of these workarounds.
If anyone has a solution to this, help is definitely appreciated.
Thank you both very much for your time.
glenngv
01-08-2003, 02:03 AM
Did you already try what I suggested?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.