madness Posted May 18, 2012 Share Posted May 18, 2012 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 More sharing options...
justsomeguy Posted May 19, 2012 Share Posted May 19, 2012 In Javascript you don't have the browser wait, like you've seen that causes it to freeze. If you want the browser to do something at a later time you can use the setTimeout or setInterval functions to schedule something to run later. http://www.w3schools.com/js/js_timing.asp Link to comment Share on other sites More sharing options...
madness Posted May 19, 2012 Author Share Posted May 19, 2012 I tried it with setTimeout function in this second example but this doesn't work, it execute whole FOR loop without pause or delay, don't know why. Link to comment Share on other sites More sharing options...
Ingolme Posted May 19, 2012 Share Posted May 19, 2012 The setTimeout won't stop the loop, it will just run a function after a specified amount of time after the setTimeout was called. Link to comment Share on other sites More sharing options...
madness Posted May 19, 2012 Author Share Posted May 19, 2012 than what will help me out there? anyone have an idea how to pause loop or give the function doSomething to do something and not freeze the firefox? Link to comment Share on other sites More sharing options...
boen_robot Posted May 19, 2012 Share Posted May 19, 2012 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 More sharing options...
madness Posted May 19, 2012 Author Share Posted May 19, 2012 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 More sharing options...
boen_robot Posted May 19, 2012 Share Posted May 19, 2012 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 More sharing options...
madness Posted May 19, 2012 Author Share Posted May 19, 2012 Its almost that, just it opens last element in "i" series "i" times. Link to comment Share on other sites More sharing options...
Ingolme Posted May 19, 2012 Share Posted May 19, 2012 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 More sharing options...
madness Posted May 19, 2012 Author Share Posted May 19, 2012 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 More sharing options...
ShadowMage Posted May 21, 2012 Share Posted May 21, 2012 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now