8 Replies - 506 Views - Last Post: 22 February 2013 - 06:18 PM

#1 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

a script on this page is causing your browser to run slowly

Posted 22 February 2013 - 03:39 AM

if it continues to run, your computer might become unresponsive.

what's wrong in this code that is causing this warning? (i did not have a problem without the while loop.)

$(document).ready(function(){
	waitForUpdate(); 
    while(1==1)
    {
		var d=new Date();
		m=d.getMinutes();
		sec=d.getSeconds();
    	if((m%10)==0 && sec>20){
    		setTimeout(waitForUpdate(),60000);     			
    	}
    }
});


Is This A Good Question/Topic? 0
  • +

Replies To: a script on this page is causing your browser to run slowly

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: a script on this page is causing your browser to run slowly

Posted 22 February 2013 - 03:45 AM

Well, as you mentioned, that while loop is responsible for causing the problem.

Well, you are looping infinite times...as 1 will always be equal to 1...and there is no loop exit condition.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#3 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: a script on this page is causing your browser to run slowly

Posted 22 February 2013 - 03:47 AM

i know its an infinite loop, nut i wait 10 min.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3561
  • View blog
  • Posts: 10,358
  • Joined: 08-June 10

Re: a script on this page is causing your browser to run slowly

Posted 22 February 2013 - 05:04 AM

there is also a problem with the setTimeout(). you donít wait 10 mins before the next waitForUpdate() is executed, because you execute that function each time you call setTimeout(). in other words, you tell setTimeout() to execute nothing (i.e. undefined) every minute. reason: setTimeout() expects a function to be given, not a function return value. i.e. it should be setTimeout(waitForUpdate, 60000);
Was This Post Helpful? 1
  • +
  • -

#5 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: a script on this page is causing your browser to run slowly

Posted 22 February 2013 - 01:41 PM

i now suspect the problem is not setTimeout (except for the syntax problem). i did wait 10 minutes to call setTimeout.

I think the cpu is busy because i keep evaluating the current time. i still couldnt find a solution to this.

what i am trying to do is to update client browsers every 10 minutes at specific times e.g., 5:10, 5:20, 5:30, etc.
Was This Post Helpful? 0
  • +
  • -

#6 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: a script on this page is causing your browser to run slowly

Posted 22 February 2013 - 01:55 PM

my workaround got rid of the problem, but i got "unspecified error?"


function areWeThereYet()
{
	var d=new Date();
	m=d.getMinutes();
	sec=d.getSeconds();
	if((m%10)==0 && sec>20){
		setTimeout(waitForUpdate,600000);  			
	}
}

$(document).ready(function(){
    waitForUpdate(); 
    while(1==1)
    {
    	setTimeout(areWeThereYet,60000);  
    }
    
});


hmm.. i did miss a '0' in wait time..

This post has been edited by g37752: 22 February 2013 - 05:25 PM

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3642
  • View blog
  • Posts: 12,637
  • Joined: 12-December 12

Re: a script on this page is causing your browser to run slowly

Posted 22 February 2013 - 03:43 PM

The code within while (1==1) is going to run and run. It will be setting the timeout, but the loop continues to run, eating up the CPU.

If you want to call a function at a regular interval you might remove the while loop and just use setInterval.
Was This Post Helpful? 1
  • +
  • -

#8 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: a script on this page is causing your browser to run slowly

Posted 22 February 2013 - 05:24 PM

View Postandrewsw, on 22 February 2013 - 03:43 PM, said:

The code within while (1==1) is going to run and run. It will be setting the timeout, but the loop continues to run, eating up the CPU.

If you want to call a function at a regular interval you might remove the while loop and just use setInterval.


so i change this:
    while(1==1)
    {
      setTimeout(areWeThereYet,600000); 
    }

to this:
setInterval(areWeThereYet,60000);


and the unspecified error is gone. i thought they should work the same?
Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3561
  • View blog
  • Posts: 10,358
  • Joined: 08-June 10

Re: a script on this page is causing your browser to run slowly

Posted 22 February 2013 - 06:18 PM

View Postg37752, on 23 February 2013 - 01:24 AM, said:

i thought they should work the same?

not at all. you infinte while() loop runs very fast, and in each cycle (in less than a millisecond) it adds the waitForUpdate() function to the timeout stack. so when the first waitForUpdate() function would be executed after ten minutes, you would have a couple of billion functions in the stack waiting to be executed, which most probably have eaten your resource way before that point. besides that your browser will be in a lock, because it cannot execute the functions while the loop is still running.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1