0

I am writing a script to display multiple world clocks in the same window. When I add the first clock its working fine, but when I add the second clock it just duplicates the first clock. It also doesn't tick.

When I try to change the zone it updates the first clock.

 <SCRIPT language="javascript">
            function addRow(tableID) {

            var table = document.getElementById(tableID);

            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);

            var colCount = table.rows[0].cells.length;

            for(var i=0; i<colCount; i++) {

            var newcell = row.insertCell(i);

            newcell.innerHTML = table.rows[0].cells[i].innerHTML;

            //alert(newcell.childNodes);
            switch(newcell.childNodes[0].type) {
            case "text":
            newcell.childNodes[0].value = "";
            break;
            case "checkbox":
            newcell.childNodes[0].checked = false;
            break;
            case "select-one":
            newcell.childNodes[0].selectedIndex = 0;
            break;
            }
         }
     }

            function deleteRow(tableID) {
            try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;

            for(var i=0; i<rowCount; i++) {
            var row = table.rows[i];
            var chkbox = row.cells[0].childNodes[0];
            if(null != chkbox && true == chkbox.checked) {
            if(rowCount <= 1) {
            alert("Cannot delete all the rows.");
            break;
            }
            table.deleteRow(i);
            rowCount--;
            i--;
             }

           }
          }catch(e) {
           alert(e);
         }
      }

     </SCRIPT>


    <script language="javascript">

    var timerRunning = false;
    var timezone = "Greenwich Mean Time";  // what time zone are you in ?
    var adjust = 0;

    function timeCheck(diff) {

            if (timerRunning) {
                clearTimeout(updatetime);
                timerRunning = false; 
            }

            gmtOffset = eval(diff);

            checkDateTime();
    }

    function checkDateTime() {

            var today = new Date();
            var year = today.getYear() + 1900;
            var month = today.getMonth()+1;
            var date = today.getDate();
            var day = today.getDay();
            var hour = today.getHours();
            var minute = today.getMinutes();
            var second = today.getSeconds();

            var lastSat = date - (day+1);
            while (lastSat < 32) lastSat+=7;
            if (lastSat > 31) lastSat+=-7;
            var firstSat = date - (day+1);
            while (firstSat > 0) firstSat+=-7;
            if (firstSat < 1) firstSat+=7;
            if ((((month == 4) && (date >= firstSat)) || month > 4) &&
            (month < 11 || ((month == 10) && day <= lastSat))) adjust += 60;
            yourOffset = (new Date()).getTimezoneOffset();
            yourOffset = yourOffset + adjust;

            var xx = navigator.appName
            var xy = navigator.appVersion;
            xy = xy.substring(0,1);
            if ((xy == 4) && (xx == "Netscape")) yourOffset = yourOffset+adjust;
            if ((((month == 4) && (date > 20)) || month > 4) && (month < 11 || ((month == 10) &&
            day < 30))) adjust -= 60;

            ourDifference = eval(gmtOffset - yourOffset);
            var half = eval(ourDifference % 60);
            ourDifference = Math.round(ourDifference / 60);
            hour = eval(hour - ourDifference);
            var m = new Array("",
            "Jan","Feb","Mar",
            "Apr","May","Jun",
            "Jul","Aug","Sept",
            "Oct","Nov","Dec");
            var leap = eval(year % 4);

            if ((half == -30) || (half == 30)) minute += 30;
            if (minute > 59) minute -= 60, hour++;
            if (minute < 0) minute += 60, hour--;
            if (hour > 23) hour -= 24, date += 1;
            if (((month == 4) || (month == 6) ||
            (month == 9) || (month == 11)) && (date==31)) date = 1, month ++;
            if (((month == 2) && (date > 28)) && (leap != 0)) date = 1, month ++;
            if ((month == 2) && (date > 29)) date = 1, month++;
            if (hour < 0) hour += 24, date --;
            if ((date == 32) && (month == 12)) month = m[1], date = 1, year++;
            if (date == 32) date = 1, month++;
            if ((date < 1) && (month == 1)) month= m[12], date = 31, year--;
            if (date < 1) date = 31, month --;
            if (((month == 4) || (month == 6) ||
            (month== 9) || (month == 11)) && (date == 31)) date = 30;
            if ((month == 2) && (date > 28)) date = 29;
            if (((month == 2) && (date > 28)) && (leap != 0)) date=28;

            for (i=1; i<13; i++) {
                    if (month == i) {
                        month = m[i]; break;
                }
            }

            var dateTime = hour;
            dateTime = ((dateTime < 10) ? "0":"") + dateTime;
            dateTime = "   " + dateTime;
            dateTime += ((minute < 10) ? ":0" : ":") + minute;
            dateTime += ((second < 10) ? ":0" : ":") + second;
            dateTime += (hour >= 12) ? " PM, " : " AM, ";
            dateTime += month + " " + date + ", " + year;

            document.getElementById("zonetime").innerHTML = dateTime;

            updatetime=setTimeout("checkDateTime()", 900);
            timerRunning = true;
    }

    </script>


     <INPUT type="button" value="Add Clock" onclick="addRow('dataTable')" />

     <INPUT type="button" value="Delete Clock" onclick="deleteRow('dataTable')" /><br/><br/>


    <TABLE id="dataTable" width="350px" border="1" align="center">
            <TR>
                 <TD><INPUT type="checkbox" name="chk"/></TD>
                <TD>
                    <select onchange="timeCheck(this.value)" size="0" align="center">
                        <option value="">SELECT --> COUNTRY</option>
                        <option value="+480">Pacific</option> // Australia UTC offset value is 10
                        <option value="+420">Central</option>
                        <option value="+300">Estern</option>
                        <option value="+600">Hawaii</option>
                        <option value="+360">Mexico</option>
                        <option value="-330">New Delhi</option>
                        <option value="-480">HONG KONG</option>
                        <option value="-540">TOKYO</option>
                        <option value="+0">London</option>
                    </select>
                </TD>
                <TD><div id="zonetime"></div></TD>
            </TR>
    </TABLE> 
4

1 回答 1

0

gmtOffset变量是全局的,并且您的时钟输出单元格在每一行上都由相同的 id 引用。这意味着每次更改时区时,都会更新具有该 ID 的第一个单元格,即第一行的最后一个单元格。

您必须确保每个时钟的 id 都是唯一的,例如<td><div id="zonetime_n"</div></td>,其中n是行号。然后,您需要将该 id 映射到其时区并进行相应更新。

于 2012-08-23T07:08:48.330 回答