Jump to content

[SOLVED] javascript crashes my firefox but not IE


EchoFool

Recommended Posts

I have a javascript which creates a live clock which works fine but after a while in FF the browsers hangs and stops responding then I have to close the program with ctrl alt del.

 

When i remove the script it never happens, and it works perfectly fine in IE with the script just not FF.

 

Was wondering if any expert here might know what I did wrong?

 

Hope you can help!

 

function live_clock()
     {
         var today  = new Date();
         var second = today.getSeconds();
         var minute = today.getMinutes();
         var hour   = today.getHours();
         var hour24 = today.getHours();
         var ampm   = "";
         
         var day    = today.getDay();
         var date   = today.getDate();
         var month  = today.getMonth();
         var year   = today.getFullYear();
         
         var days   = new Array();
          days[0]   = "Sunday";
          days[1]   = "Monday";
          days[2]   = "Tuesday";
          days[3]   = "Wednesday";
          days[4]   = "Thursday";
          days[5]   = "Friday";
          days[6]   = "Saturday";
          
         var mns   = new Array();
          mns[0]   = "January";
          mns[1]   = "February";
          mns[2]   = "March";
          mns[3]   = "April";
          mns[4]   = "May";
          mns[5]   = "June";
          mns[6]   = "July";
          mns[7]   = "August";
          mns[8]   = "September";
          mns[9]   = "October";
          mns[10]  = "November";
          mns[11]  = "December";
           
        if(second<10)
            {
                second = "0"+second;
            }
            
        if(minute<10)
            {
                minute = "0"+minute;
            }
            
        if(hour24<=12)
            {
                ampm = "AM";
            }
            
        else
            {
                ampm = "PM"
            }
            
        if(hour24>=12)
            {
                hour= hour-12;
            }
            
        if(hour24==0)
            {
                hour=12;
            }
        
             document.getElementById('live_clock').innerHTML=(hour+":"+minute+":"+second+" "+ampm+'<br>'+days[day]+', '+date+' '+mns[month]+' '+year);
		 setInterval('live_clock()', 1000);
    }
		window.onload = live_clock;

 

Thats the JS script ^ in my html page i have:

 

<div align=center>
<div id="live_clock" align="center" class=clock style="background-color: #ffffff;max-width:150px;">
</div>

 

Hope you can help me out! :)

Link to comment
Share on other sites

Well, either IE is dumb, or it is smart. Either way, the problem is that you're using setInterval and you should use setTimeout.

 

setInterval runs that program every x milliseconds, while setTimeout runs it ONCE in x milliseconds. So, by using setTimeout inside your function, you're running the program, then running it every second. But then every second you run it again. So, it's running once, then twice, then four times, then eight times, then sixteen, and so on, all at the same time. That is bombing FF. So, either just change setInterval to setTimeout, OR remove that from your function and change the window.onload to setInterval.

 

Make sense?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.