Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
setInterval() crashes Firefox
Old 07-24-2008, 08:21 PM [RESOLVED] setInterval() crashes Firefox
Experienced Talker

Posts: 33
Trades: 0
Greetings,

The following JavaScript code displays a clock in a <p id="time"></p> element on a web page and updates the clock once per minute.

When letting this code run for ~10 minutes under FireFox 2.0.0.16 on Ubuntu 6.10 and Mac OS X, Firefox eventually becomes unresponsive. A CPU check shows Firefox consuming +100% of the CPU.

Am I losing my mind or did I code something incorrect?
I'd appreciate your thoughts. Thanks.

Code:
window.onload = function() {
    
    /*
    * Execute the following functions in the order they appear after the webpage loads.
    * The reason being, the DOM needs to finish loading in order for these functions to
    * manipulate certain elements.
    */

    displayTime();
}


function displayTime() {
    var date = new Date();
    var hour = date.getHours();  // Returns 0-23.
    var minutes = date.getMinutes(); // Returns 0-59.

    // Is it AM or PM?
    if (hour >= 12) {
        var ampm = "PM";
    } else {
        ampm = "AM";
    }

    // Convert hours format from 24 hour to 12 hour format.
    if (hour > 12) {
        hour = hour - 12;
    }

    // Convert hour 0 to 12.
    if (hour == 0) {
        hour = 12;
    }

    // Convert minutes format to mm for 0-9.
    if (minutes < 10) {
        minutes = "0" + minutes;
    }

    // Put everything together to form the current time.
    var time = hour + ":" + minutes + " " + ampm;

    // Display the time on the webpage.
    var paraTime = document.getElementById("time");

    if (paraTime.childNodes.length > 0) {  // Meaning it has childNodes. hasChildNodes doesn't work.
        paraTime.removeChild(paraTime.firstChild);
    }

    paraTime.appendChild(document.createTextNode(time));

    // Update the clock every one minute.
    setInterval(displayTime, 60000);
}

Last edited by nick1; 07-24-2008 at 10:13 PM..
nick1 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-24-2008, 08:51 PM Re: setInterval() crashes Firefox
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
Change setInterval to setTimeout the way you are doing it. Or move the setInterval to the onload function.

What's happening?
the setInterval will call the function over and over. The prob, the end of the function starts ANOTHER interval. So, everytime the function runs you are stacking up another one. After 10 minutes, you've got 11 intervals running, all trying to mess with the dom.

BTW, 100% CPU is not the same as crashing. If Firefox crashed, it would not be running anymore.
__________________

Please login or register to view this content. Registration is FREE


Last edited by willcode4beer; 07-24-2008 at 08:52 PM..
willcode4beer is offline
Reply With Quote
View Public Profile
 
Old 07-24-2008, 10:12 PM Re: setInterval() crashes Firefox
Experienced Talker

Posts: 33
Trades: 0
I'm embarrassed, haha.

Thank you for your quick reply and, most importantly, helping me understand why my code was behaving like it was.

I decided to replace setInterval with setTimeout, since setTimeout executes displayTime once and then stops.

Thanks again for your help.
nick1 is offline
Reply With Quote
View Public Profile
 
Old 07-25-2008, 10:38 AM Re: setInterval() crashes Firefox
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
This is an important lesson that I've had to learn the hard way, also.
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to setInterval() crashes Firefox
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.19811 seconds with 12 queries