PDA

View Full Version : pause a function and return after doing task



jscheuer1
03-16-2005, 06:53 AM
My question is:

Is there any way to accomplish this:

function main_function(){
do this;
do that;
call another function and wait until it is finished before proceeding;
do this;
do that;
{
in JavaScript?

ddadmin
03-16-2005, 07:33 AM
There's a few ways to do this that I can think of (no magic line though), but it really depends on what main_function() is trying to do. If you roughly describe the scenerio, that would be helpful in coming up with something.

jscheuer1
03-16-2005, 08:13 AM
Thanks for the offer. The script I have in mind is long and complex, though the actions I wish to accomplish are not. The function I want to call is:

function test_preload(imgobj){
xtd=imgobj
testimage=true;
testimage=xtd.complete;
if (testimage!==false)
return;
setTimeout("test_preload(xtd)",2000);}
The function I am calling it from is long and complex. It sets borders, backgrounds, dimensions and such but, the key lines in it that I want to temporarily break out of and run my test_preload() from are the lines that take images already preloaded or still preloading (depending on the connection speed) and display them for the first time, like this:

B_LI.src=B_Pre_Img[0].src;
where B_LI.src is the source (src=) of a named image tag, B_Pre_Img[0] is an already declared image Object (new Image()) and B_Pre_Img[0].src is a .jpg that has already been defined as the source for this image Object. I know all this is working because the script without the test_preload() function works fine and does all I describe (incidently it works fine with it, just doesn't perform the test). The trouble arises with slow connections which haven't cached all the images yet. So, what I have now that doesn't do what I want is this:

test_preload(B_Pre_Img[0]);
B_LI.src=B_Pre_Img[0].src;
There are no errors given in either IE6 or NS7.2 but, it is obvious from testing on a speed simulator that even if my test_preload() function is being executed (probably is) the code of the function I am calling it from is continuing on without waiting to see what happened. If this isn't clear enough, I can attach a .zip of the entire script, it is too long to paste in.

ddadmin
03-16-2005, 08:40 AM
Ok, I have a fairly good idea of what you have in mind. Basically to pause a function at a certain point until another function within it has executed completely looks somethng like:


var alreadyrun=false //indicate whether top portions of function has already executed once
var approved=false //indicate whether it's ok to execute portions following another function within

function masterfunction(){
if (!alreadyrun){
//do this
/do that
alreadyrun=true
}
test_preload(B_Pre_Img[0])
if (approved){ //if test_preload() sets approve to true
//do this
/do that
}
else //else, re-run masterfunction() again
setTimeout("masterfunction()", 50)
}

masterfunction()

The key here is to continously run masterfunction() until test_preload() has finished with what it's doing (in this case, preload images I gather). Now, to avoid masterfunction() re-running multiple times its lines of codes, variable "alreadyrun" is used to indicate when the code leading up to test_preload() has already run once. Variable "approved" tells the script whether it's ok to continue executing the lines following test_preload(), or when test_preload() has completed its task. This means you need to have test_preload() set approved to true once it's done.

I haven't field tested the above code, but it should work. FYI the IFRAME scroller (http://www.dynamicdrive.com/dynamicindex2/iframe-scroller.htm) relies on this technique to only start scrolling when it has determined the height of the document contained inside. Since using "window.onload" is unreliable to detect when it's OK to probe the document inside for its height, I had to continously scan the height in real time until it's a valid number (instead of 0), and start the script once that number is retrieved.

jscheuer1
03-16-2005, 09:26 AM
Not exactly what I was looking for but, you knew that, and I think it will help me get this thing done, it's a nice technique to have in one's arsenal, thanks. What I was really looking for is something like 'call' in a batch file. I was pretty sure there was nothing like that because, well, I'd have run across it by now. I'm half tempted to rewrite the whole thing (it is from a question submitted here) as I have a simple slide show script I wrote that does what I'm talking about but, the check is integrated directly into the flow of the script. I'll take a look at that IFRAME scroller too. Thanks again!

ddadmin
03-16-2005, 09:36 AM
No problem, and feel free to provide more details and some of us can help come up with more specific solutions if possible. In JavaScript there really isn't a "pause" feature, but I'm fairly confident it can be simulated for most intents and purposes. Only you completely know what you're trying to accomplish though, so the solution is very specific...

jscheuer1
03-16-2005, 04:15 PM
More detail. I originally got the code I'm trying to mod with this test_preload() function when I checked out this thread:

photo gallery problem for dial up users (http://www.dynamicdrive.com/forums/showthread.php?p=6506#post6506)

and tried to solve the problem described there. It looked like a slideshow preloading images, then not checking to see if the images were loaded before using them. Assuming the problem hasn't been solved yet (and even, perhaps, if it has) you can get a good look at what is involved by following this link (http://www.line-man.com/photo.html) mentioned at the beginning of that thread and checking its source.

Interestingly enough, it probably is one of the few times DD has been credited with a script not found in its library.

jscheuer1
03-16-2005, 05:32 PM
The more I look at this, the less it looks like a job for what you mentioned:

if (!alreadyrun){
//do this
/do that
alreadyrun=true
}
test_preload(B_Pre_Img[0])
if (approved){ //if test_preload() sets approve to true
//do this
/do that
}
else //else, re-run masterfunction() again
setTimeout("masterfunction()", 50)
}as it really does have to be like a DOS batch file 'call'. It (the test_preload() function) needs to be available at several points in the main function and then again in another function that is already a repeating function. The logic won't support it. That is why I am so tempted to simply rewrite the entire script. The only thing holding me back is, the work involved and the fact that it is not for me, coupled with the fact that the original poster on this hasn't gotten back to us about it.