0

我的英语很差,所以我会犯错误。

两天前我写了时钟区,但效果很差。往下看 你可以在那里看到我的代码。我需要你的建议。

<script>
        function Clock(time_zone) {
            var time    = new Date();
            var hours   = time.getUTCHours() + time_zone;
            var minutes = time.getUTCMinutes();
            var seconds = time.getUTCSeconds();
            var suffix  = "AM";
            var day = time.getDate();
            var month = time.getMonth();
            var year = time.getFullYear();
            var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");


            if (hours < 0) {
                hours += 24;
            }

            if (hours > 11) {
                suffix = "PM";
                hours -= 12;
            }

            if (hours == 0) {
                hours = 12;
            }

            if (hours < 10) {
                hours = "0" + hours;
            }

            if (minutes < 10) {
                minutes = "0" + minutes;
            }

            if (seconds < 10) {
                seconds = "0" + seconds;
            }

            var time_span = document.getElementById('time');
            var suffix_span = document.getElementById('suffix');
            var date_span = document.getElementById('date');
            time_span.innerHTML = hours + ":" + minutes + ":" + seconds;
            suffix_span.innerHTML = suffix;
            date_span.innerHTML = day + " " + months[month] + " " + year;

            setTimeout(function(){Clock(time_zone)}, 1000);
        }
        window.onload = function()
        {
            Clock(2);
        }
    </script>

这是更改区域的按钮。

<input type="button" onClick="Clock(5)" value="AMSTERDAM" name="AMSTERDAM"/>

当我单击此按钮时,我的时间在旧区域时间(时钟(2))和新区域时间(时钟(5))之间闪烁

能告诉我怎么修吗???

感谢您的所有建议。

拜托,你会改善我的语言错误。

4

2 回答 2

2

需要取消原来的超时

if(window.timer) window.clearTimeout(window.timer);
window.timer = setTimeout(function(){Clock(time_zone)}, 1000);
于 2013-06-26T19:31:43.450 回答
0

因为你有两个时钟方法正在运行

  1. 当您加载页面时,clock(2) 正在运行,并且在 setTimeOut 中再次调用它。所以它是一个循环。

  2. 然后当您单击另一个时钟时(5)以相同的方式运行。

所以在任何时候调用clock方法的时候都需要取消上一个。

这是小提琴

var t;

 function Clock(time_zone) {

       clearTimeout(t);
       var time = new Date();
       var hours = time.getUTCHours() + time_zone;
       var minutes = time.getUTCMinutes();
       var seconds = time.getUTCSeconds();
       var suffix = "AM";
       var day = time.getDate();
       var month = time.getMonth();
       var year = time.getFullYear();
       var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");


       if (hours < 0) {
           hours += 24;
       }

       if (hours > 11) {
           suffix = "PM";
           hours -= 12;
       }

       if (hours == 0) {
           hours = 12;
       }

       if (hours < 10) {
           hours = "0" + hours;
       }

       if (minutes < 10) {
           minutes = "0" + minutes;
       }

       if (seconds < 10) {
           seconds = "0" + seconds;
       }

       var time_span = document.getElementById('time');
       var suffix_span = document.getElementById('suffix');
       var date_span = document.getElementById('date');
       time_span.innerHTML = hours + ":" + minutes + ":" + seconds;
       suffix_span.innerHTML = suffix;
       date_span.innerHTML = day + " " + months[month] + " " + year;

      t =  setTimeout(function () {
           Clock(time_zone)
       }, 1000);
   }

   window.onload = function () {
       Clock(2);
   }
于 2013-06-26T19:32:48.057 回答