Jump to content

wait function or something like that


madness
 Share

Recommended Posts

I need some wait function for javascript executed by firefox. I tried to use this

function pausecomp(ms) {ms += new Date().getTime();while (new Date() < ms){}}

but firefox freezes every time. After this I made something like

var a;for loop{a=0;   executing somethingdoSomething();}doSomething(){if (a!=1){		    a=1;		    var delay = Math.floor((Math.random()*5000)+1000);		    t=setTimeout("doSomething()",delay);	    }else		    return;}

but this do not work. It just execute FOR loop without any delay. I need something that enter random wait time in FOR loop.

Link to comment
Share on other sites

Simple - place the loop in the timeout:

var delay = Math.floor((Math.random()*5000)+1000);setTimeout(function() {    for (; {        //Executing something heavy    }}, delay);//Executing something light

Link to comment
Share on other sites

I think this will also not help me. I have exactly this:

for (i=1;i<name.length-1;i++){win[i]=window.open(some link+i);}

If I put calling of first function in this topic, pausecomp function, into for loop all works fine, but freezes firefox. I need something to make pause between opening this windows, but windows need to have index of i.

Link to comment
Share on other sites

Set different timeout times then... like

var win = [];function openWindow(i) {    win[i] = window.open(some link+i);}for (i=1;i<name.length-1;i++){    setTimeout(openWindow, 1000+(i*100), i);}

Link to comment
Share on other sites

I don't think all browsers support the third parameter od the setTimeout function.This should fix it (I think):

setTimeout( function() { openWindow(i); }, 1000 + (i*100) );

If not, you'll need to attach the value of i to somewhere else.

Link to comment
Share on other sites

I just need it for firefox. In for loop I have a link that is made from element of one series. While I enter elements, they are stored in one series. "i" is the index of every element so I need to open first window with link that have first element, second window that contains second element. In last robot post my windows are opening with delay but it opens "i" times with last element in series, since for pass all elements before any window is open.

Link to comment
Share on other sites

You need to use something called a closure. Basically, what's happening is that by the time the setTimeout expires and the code runs, the loop is done and i is equal to whatever it was after the last iteration of the loop. This is why you are seeing the last element opening in each window. A closure prevents this by passing the state of 'i' into the setTimeout code. It should look something like this if my memory serves:

setTimeout( function(win) { return function() { openWindow(win); } }(i), 1000 + (i*100) );

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...