设置间隔秒快于本地时间秒

时间:2011-11-18 11:55:24

标签: jquery

我使用jquery制作了一个时钟。这很好。现在我注意到,时间与我的实际系统时间不同,而我的时钟启动它很好。如果我的时钟运行很长时间,我会从系统时间变化。

我的职责是:

$(document).ready(function(){

                var paper = new Raphael(document.getElementById('USClock'),500,500);

                var secondHand = paper.path('m 250,250, l 0 -200');
                var minuteHand = paper.path('m 250,250, l 0 -200');
                var hourHand = paper.path('m 250,250, l 0 -150')
                minuteHand.attr({'stroke':"#f00",'stroke-width':5});
                hourHand.attr({'stroke':"#0ff",'stroke-width':10})

                 var myDate = new Date();
                 var actualSeconds = myDate.getSeconds();
                 var actualMinutes = myDate.getMinutes();
                 var actualHours = myDate.getHours();
               // 

               var currentSecondDeg = actualSeconds*6;
               var currentMinuteDeg = actualMinutes*6;
               var currentHourDeg  =  actualHours*30 + ((actualMinutes)*0.5);

               var num = currentSecondDeg;

               secondHand.rotate(currentSecondDeg,250,250);
               minuteHand.rotate(currentMinuteDeg,250,250);
               hourHand.rotate(currentHourDeg,250,250);

               var update = function(){
                    num +=6;
                    currentSecondDeg = num;
                    secondHand.rotate(currentSecondDeg,250,250);
                    if(( currentSecondDeg % 360) == 0){
                        num = 0;
                        currentMinuteDeg +=6;

                        minuteHand.rotate(currentMinuteDeg,250,250);

                        currentHourDeg += 0.5; // 360/12 = 30deg per hour so per min 30/60 = 0.5

                        hourHand.rotate(currentHourDeg,250,250);

                        if(currentMinuteDeg == 360){
                            currentMinuteDeg = 0;
                        }

                        if(currentHourDeg == 360){
                            currentHourDeg = 0;
                        }
                    }
                    //$('p').html('SecondsDeg'+(currentSecondDeg)+'<br> currentMinuteDeg'+currentMinuteDeg+'<br>currentHourDeg'+currentHourDeg);
               }
               var myInterval = setInterval(update,1000);
            })

实际上我的问题是为什么设置间隔会从长时间过程中变化。如果任何人发现我的功能的问题或错误的工作方式让我知道。否则,如果有人知道一个很好的方式做得赞赏。

提前谢谢!任何解决方法,否则我怎么能在我的网站上显示4个不同的时钟?从服务器获取时间的唯一方法是什么?

2 个答案:

答案 0 :(得分:2)

setInterval()(以及setTimeout())可能会受到处理器速度,浏览器javascript实施速度以及处理器负载的影响。它们与系统时钟无关。

阅读this page以获取更多信息。

答案 1 :(得分:1)

我认为你给setInterval一点点功劳 - 看看这个link

基本上,类似于setTimeout,setInterval“也可以在页面(或OS /浏览器本身)忙于其他任务时触发。”

相关问题