Javascript: setInterval causing browser to hang and crash (Firefox)?

I've been experimenting a bit, taught myself some Javascript within the past few days, and I found it surprisingly easy, I guess after learning my first programming language others are easier to learn than if it were my first...

Anyway, I made a clock basically... it gets the date, stores it in a variable, and replaces the paragraph with an assigned id (<p id="demo"; <script> function() { document.getElementById("demo").innerHTML=[insert code];) Stuff like that... well, in the clock, I have setInterval to execute the function to display the current time every 1000ms... I also made additional stuff like getting the individual values (hours, minutes, seconds, AM/PM) but to keep them updated I had to use setInterval to keep them updated... however, I have four setIntervals going at 1000ms and it causes the browser to hang. What's a way to have four different javascript functions going off at a set time, two at the same time, two at different assigned times?

2013-02-14T21:00:41Z

I have three because although the timer is running at 1000ms, if I try to get seconds, it won't update with the original clock. Is it possible to get the time from one function and use the variable and value to carry on to the next function? I'll link to the calculator


https://dl.dropbox.com/u/113810822/clock.html

2013-02-14T21:02:49Z

The program causes Google Chrome's process to increase by thousands and multiplies every second. Went from 450K to 800K in a minute

Ratchetr2013-02-14T20:55:55Z

Favorite Answer

>> I made a clock basically.
>> I have four setIntervals going at 1000ms

Why? You don't need 4 calls to setinterval to do a clock. You need 1. You can compute everything you need to know in each callback. Hours, minutes, seconds, AM/PM. You do all of that in a single callback. Not 4.

That being said, FF shouldn't hang or crash. Maybe FF has a bug? Or, more likely, your code has a bug. You might be stuck in a loop. If you wait long enough, (90 seconds should be long enough), does FF tell you that JavaScript is taking a long time to run? Continue or cancel? That is what it should do. Just hanging is wrong.

But start by fixing your JS so you only need one call to setinterval. 4 is 3 too many.

Anonymous2014-08-15T23:02:37Z

CCleaner is a freeware system optimization and privacy tool. It removes unused files from your system - allowing Windows to run faster and freeing up valuable hard disk space. It also cleans traces of your online activities such as your Internet history. CCleaner also fixes your registry. But the best part is that it's fast (normally taking less than a second to run) and contains NO Spyware or Adware.

You can download cleaner for free here http://bit.ly/1kGZmoH

WK of Angmar2013-02-15T07:37:44Z

This is certainly caused by a bug in your code. Review the code that you have written, use a debugger and go through it step-by-step. Find the fault (probably some sort of infinite or multiple looping issue) and fix it.