J. Otto: Digitaleuhr mit Römischenzahlen stürtz bei Mozilla immer ab?

Mein Javaspript "Digitaleuhr mit Römischenzahlen" stürzt bei Mozilla immer ab? Un im Explorer gehts? Warum? (is klar ich bin zu blöd) Aber waskann ich besser machen?

<meta http-equiv="Content-Language" content="de">  
<script type="text/javascript">  
<!--  
function start() {  
 time();  
 window.setInterval("time()", 1000);  
}  
function time() {  
 var now = new Date();  
 hours = dec2rom(now.getHours());  
 minutes = dec2rom(now.getMinutes());  
 seconds = dec2rom(now.getSeconds());  
  
  
    hoursb = now.getHours();  
 minutesb = now.getMinutes();  
 secondsb = now.getSeconds();  
  
  
      function dec2rom (dec)  
  {  
    var dec = '' + dec;  // Converts the decimal "dec" to a string  
    while (dec.substr(0, 1) == '0')  { dec = dec.substring(1, length); }  // Remove zeroes in front to avoid converting to the octal system  
    var num = parseInt(dec);  // Converting string to decimal  
    var thousand = 0;  // Counts, how often we substracted 1000 from the decimal  
    while (num > 1000)  { num -= 1000; thousand++; }  // Required to lower the number  
    dec = num + '';  // Overwrites "dec" with the new - under circumstances lower - number  
    var rN = '';  // Prepare empty string (the result)  
    var length = dec.length;  // Length of the string  
    var adds = new Array('I', 'X', 'C', 'M');  // These are letters which can be placed next to eacht other (8 = "VIII"), sorted asc.  
    var noAdds = new Array('V', 'L', 'D');  // These letters cannot be put next to each other ("VV" does not exist), sorted asc.  
  
    for (var i = 0; i < length; i++)  // This loop will extract each digit from the decimal and handle its "translation"  
    {  
      var d = dec.substr(i, 1);  // Actual digit of "dec"  
      var pos = length - 1 - i;  // Reverse position in arrays  
      if (d == 0)  { continue; }  // The Romans did not know the zero  
      else if ( (d == 1) || (d == 2) || (d == 3) )  // Letters, that are added (like "III")  
      {  
        for (var j = 0; j < d; j++)  { rN += adds[pos]; }  // A loop that addes 1, 2 or 3 times the given letter (like "III", "XXX")  
      }  
      else if (d == 4)  { rN += adds[pos] + noAdds[pos]; }  // Adds two letters, if the digit is "4"; in case of 4: "IV"  
      else if (d == 5)  { rN += noAdds[pos]; }  // Simply add the non-addable signs ("V", "L", "D")  
      else if ( (d == 6) || (d == 7) || (d == 8) )  // Comparable to case "1", "2" or "3"  
      {  
        rN += noAdds[pos];  // Sign, that is used in case "5"  
        for (var k = 5; k < d; k++)  { rN += adds[pos]; }  // 1, 2 or 3 the given letter (like "III", "XXX")  
      }  
      else if (d == 9)  { rN += adds[pos] + adds[pos + 1]; }  // Comparable to "4", but only with addable letters  
    }  
  
    for (var m = 0; m < thousand; m++)  { rN = 'M' + rN; }  // At beginning, we substracted "thousand" times 1000; now, we add them  
  
   Ergebnis = rN.toUpperCase();  // Writes the Roman string in capital letters in the Roman text input  
   return Ergebnis;  
  }  
 thetime = (hours < 10) ? "0" + hours + ":" : hours + ":";  
 thetime += (minutes < 10) ? "0" + minutes + ":" : minutes + ":";  
 thetime += (seconds < 10) ? "0" + seconds : seconds;  
  
 element = document.getElementById("time");  
 element.innerHTML = thetime;  
    thetimeb = (hoursb < 10) ? "0" + hoursb + ":" : hoursb + ":";  
 thetimeb += (minutesb < 10) ? "0" + minutesb + ":" : minutesb + ":";  
 thetimeb += (secondsb < 10) ? "0" + secondsb : secondsb;  
  
 element = document.getElementById("timeb");  
 element.innerHTML = thetimeb;  
  
  
}  
  
//-->  
</script>  
</head>  
<body onload="start();">  
  <center>  
  
<table border="1" cellspacing="1" style="border-collapse: collapse" bordercolor="#111111" id="AutoNumber1">  
  <tr>  
    <td width="100%">  
  
    <table border="0" cellspacing="0" bordercolor="#111111"  cellpadding="0" style="border-collapse: collapse">  
  <tr>  
      <td width="124" height="124" >&nbsp;</td>  
      <td width="200" height="124" >&nbsp;</td>  
      <td width="124" height="124" >&nbsp;</td>  
    </tr>  
  <tr>  
      <td width="124" height="200" >&nbsp;</td>  
      <td width="200" height="200" border="1" style="border-style: solid; border-width: 1">  
      <font face="Arial" size="2">  
      <p id="time"  align="center">  
      Text  
      <p id="timeb" align="center">Text</p>  
      </font>  
      </td>  
      <td width="124" height="200" >&nbsp;</td>  
    </tr>  
  <tr>  
      <td width="124" height="124" >&nbsp;</td>  
      <td width="200" height="124" >&nbsp;</td>  
      <td width="124" height="124" >&nbsp;</td>  
    </tr>  
</table>  
  
</td>  
  </tr>  
</table>  
</center>
  1. Hallo,

    Tu mir einen Gefallen und poste JS auch als JS!!
    Ich probiers mal, damit ich den QT mal analysieren kann :)

    function start() {
    time();
    window.setInterval("time()", 1000);
    }
    function time() {
    var now = new Date();
    hours = dec2rom(now.getHours());
    minutes = dec2rom(now.getMinutes());
    seconds = dec2rom(now.getSeconds());

    hoursb = now.getHours();
    minutesb = now.getMinutes();
    secondsb = now.getSeconds();

    function dec2rom (dec)
      {
        var dec = '' + dec;  // Converts the decimal "dec" to a string
        while (dec.substr(0, 1) == '0')  { dec = dec.substring(1, length); }  // Remove zeroes in front to avoid converting to the octal system
        var num = parseInt(dec);  // Converting string to decimal
        var thousand = 0;  // Counts, how often we substracted 1000 from the decimal
        while (num > 1000)  { num -= 1000; thousand++; }  // Required to lower the number
        dec = num + '';  // Overwrites "dec" with the new - under circumstances lower - number
        var rN = '';  // Prepare empty string (the result)
        var length = dec.length;  // Length of the string
        var adds = new Array('I', 'X', 'C', 'M');  // These are letters which can be placed next to eacht other (8 = "VIII"), sorted asc.
        var noAdds = new Array('V', 'L', 'D');  // These letters cannot be put next to each other ("VV" does not exist), sorted asc.

    for (var i = 0; i < length; i++)  // This loop will extract each digit from the decimal and handle its "translation"
        {
          var d = dec.substr(i, 1);  // Actual digit of "dec"
          var pos = length - 1 - i;  // Reverse position in arrays
          if (d == 0)  { continue; }  // The Romans did not know the zero
          else if ( (d == 1) || (d == 2) || (d == 3) )  // Letters, that are added (like "III")
          {
            for (var j = 0; j < d; j++)  { rN += adds[pos]; }  // A loop that addes 1, 2 or 3 times the given letter (like "III", "XXX")
          }
          else if (d == 4)  { rN += adds[pos] + noAdds[pos]; }  // Adds two letters, if the digit is "4"; in case of 4: "IV"
          else if (d == 5)  { rN += noAdds[pos]; }  // Simply add the non-addable signs ("V", "L", "D")
          else if ( (d == 6) || (d == 7) || (d == 8) )  // Comparable to case "1", "2" or "3"
          {
            rN += noAdds[pos];  // Sign, that is used in case "5"
            for (var k = 5; k < d; k++)  { rN += adds[pos]; }  // 1, 2 or 3 the given letter (like "III", "XXX")
          }
          else if (d == 9)  { rN += adds[pos] + adds[pos + 1]; }  // Comparable to "4", but only with addable letters
        }

    for (var m = 0; m < thousand; m++)  { rN = 'M' + rN; }  // At beginning, we substracted "thousand" times 1000; now, we add them

    Ergebnis = rN.toUpperCase();  // Writes the Roman string in capital letters in the Roman text input
       return Ergebnis;
      }
    thetime = (hours < 10) ? "0" + hours + ":" : hours + ":";
    thetime += (minutes < 10) ? "0" + minutes + ":" : minutes + ":";
    thetime += (seconds < 10) ? "0" + seconds : seconds;

    element = document.getElementById("time");
    element.innerHTML = thetime;
        thetimeb = (hoursb < 10) ? "0" + hoursb + ":" : hoursb + ":";
    thetimeb += (minutesb < 10) ? "0" + minutesb + ":" : minutesb + ":";
    thetimeb += (secondsb < 10) ? "0" + secondsb : secondsb;

    element = document.getElementById("timeb");
    element.innerHTML = thetimeb;
    }

      
    mfg, Flo
    
    -- 
    Developers are dying. Computers are getting trash. CEO's become forgetten. The only remaining things are ideas, lies and crises. So if you want to be immortal, first think, than stop it and go to microsoft and become later a manager at Lehman Brothers...  
    sh:) fo:| ch:? rl:( br:^ n4:| ie:{ mo:| va:} de:> zu:} fl:{ ss:) ls:< js:|  
    \*Zu dem de:> Ich benutze wegen IE im moment noch tabellen, weil dieser display:table noch nicht versteht. Ich werde aber, wenn IE 6 & IE 7 < 10% mein neues CSS-Layout einspielen...
    
    1. Hallo,

      du definierst dec2rom nachdem du sie benutzt.

      mfg, Flo

      --
      Developers are dying. Computers are getting trash. CEO's become forgetten. The only remaining things are ideas, lies and crises. So if you want to be immortal, first think, than stop it and go to microsoft and become later a manager at Lehman Brothers...
      sh:) fo:| ch:? rl:( br:^ n4:| ie:{ mo:| va:} de:> zu:} fl:{ ss:) ls:< js:|
      *Zu dem de:> Ich benutze wegen IE im moment noch tabellen, weil dieser display:table noch nicht versteht. Ich werde aber, wenn IE 6 & IE 7 < 10% mein neues CSS-Layout einspielen...
  2. dein dec2rom ist recht kompliziert, darin liegt wohl der fehler, mein FF bleibt nach jeder IX stehen.

    Diese Variante ist kürzer und stabiler:

      
    function dec2rom (x) {  
     var r="";  
     var n=[1000,"M",900,"CM",500,"D",400,"CD",100,"C",90,"XC",50,"L",40,"XL",10,"X",9,"IX",5,"V",4,"IV",1,"I"];  
     for (var i=0; i<n.length; i+=2) {  
      while (n[i]<=x) {  
       x-=n[i];  
       r+=n[i+1];  
      }  
     }  
     return (r=="")?"nihil":r;  
    }  
    
    
    1. Danke dann kann ich es auch noch mit millisekunden machen sicht lustig aus.