3
function theFunction(a){
    var newNum = a+1;
    var iframe = document.createElement("iframe");
    var currURL = urlArray[a];
    iframe.src = currURL;
    iframe.frameBorder = "0";
    iframe.height = "0";
    iframe.width = "0";

iframe.onload = function(){
    document.getElementById('number').innerHTML = a;
};
    var arrayLength = someArray.length;

    if(a != arrayLength){
     theFunction(newNum);
     }
}

This is my basic function that is recursive through an array. Anyways, sometimes as it goes through (during the //do nothing part). It will get stuck, and I want it to timeout after say 2 seconds AND move onto the next one. Is this possible to do, or will I just have to completely stop the function?

EDIT: Set timeout might not be what I am looking for, but some way to just stop the function and move to the next one.

EDIT 2: Added more to the code

10
  • What exactly do you want to have happen? Have your function "pause" for 2 seconds before it does the next recursive call? Commented Nov 18, 2011 at 2:56
  • ideally I would like for it to just run normally, but if it notices the function takes more than 2 seconds to run, it skips to the next iteration. Commented Nov 18, 2011 at 2:57
  • As far as I know, this isn't possible in Javascript, though I may be wrong. Commented Nov 18, 2011 at 2:59
  • Is there possibly another reason for it getting "stuck" that you can debug for? I can't imagine a function such as this getting stuck unless it gets put into an infinite loop. I feel there must be a workaround without using setTimeOut(). Please inform us of exactly what else is going on in this code, such as array values and so forth. Commented Nov 18, 2011 at 3:01
  • Yea, during the commented section it is loading external pages. Usually (95% of the time) it loads in fractions of a second and works perfectly, but sometimes it finds a page that is slow/large file/missing/whatever and may take longer to load. For those pages I want it to just skip and move on. Commented Nov 18, 2011 at 3:04

2 Answers 2

1

Hmmm. Do you know exactly what's happening when it get "stuck"? Because if "stuck" means hitting a loop or some other error then setTimeout won't help you. In my experience, code set to execute with setTimeout will happen when the given delay has passed or when other code has stopped executing, whichever happens LAST. In other words, I don't think you can interrupt currently running code with a setTimeout, which seems to be what you're asking.

I know that's sort of a non-answer; I suggest nailing down the nature of the stuckness instead.

Sign up to request clarification or add additional context in comments.

2 Comments

Yea, settimeout might not be what I am looking for. Just some way to stop the current function and move to the next one. And for what makes it stuck, during the commented section it is loading external pages. Usually (95% of the time) it loads in fractions of a second and works perfectly, but sometimes it finds a page that is slow/large file/missing/whatever and may take longer to load. For those pages I want it to just skip and move on.
Thoughtful handling of the XMLHttpRequest object may be helpful. Frameworks (e.g. jQuery) abstract this, usually only giving you the ability to make callbacks execute when the ajax call has returned. If you monitor the XMLHttpRequest object directly, you might be able to finesse this kind of thing. Never tried it though.
0

You could "try" to use iframe.contentDocument.readyState, but I say "try" with little confidence because with my tests it always came out to the "complete" state, but possibly when you run in to some pages that take awhile to load they may stay in some of the previous states.

For more information on this and the different states view this

http://msdn.microsoft.com/en-us/library/ms534358%28v=vs.85%29.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.