setInterval()用于模拟时钟 [英] setInterval() for an analogue clock

查看:186
本文介绍了setInterval()用于模拟时钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JavaScript还是很陌生,在使用 etInterval()时遇到了麻烦。



我正在创建一个模拟时钟作为练习。我有一个函数 setTime(),该函数通过相应地旋转箭头来获取并显示时间。



我想一次又一次地执行此功能。



这是我的代码:

  $(document).ready (function(){

function setTime(){
var d = new Date();
var hour = d.getHours();
var minutes = d.getMinutes();
var hourRotation =小时* 30 +(分钟/ 2);
var minutesRotation =分钟* 6;
$(#small)。css({ b $ b -webkit-transform:旋转( + hourRotation + deg),
-moz-transform:旋转( + hourRotation + deg),
-o-transform: rotate( + hourRotation + deg)
});
$(#big)。css({
-webkit-transform: rotate( + minutesRotation + deg),
-moz-transform: rotate( + minutesRotation + deg),
-o-transform: rotate( + minutesRotation + deg)
});
};
函数clockStart(){
setInterval(setTime(),1000);
setTime();
}
clockStart();
});

我想了解这种方法及其使用方法。我能找到的示例看起来很明显,但仍然无法使它起作用。

解决方案

您正在 setInterval()中进行函数调用。 / p>

您要执行的操作是:

  setInterval(setTime,1000 ); 


I'm quite new to JavaScript and I have trouble working with etInterval().

I'm creating an analogue clock as an exercise. I have a function setTime() that gets and displays the time by rotating the arrows accordingly.

I want to execute this function again and again.

Here is my code :

    $(document).ready(function(){

        function setTime(){
            var d = new Date();
            var hour = d.getHours();
            var minute = d.getMinutes();
            var hourRotation = hour * 30 + (minute / 2);
            var minuteRotation = minute * 6;
            $("#small").css({
                "-webkit-transform": "rotate(" + hourRotation + "deg)",
                "-moz-transform": "rotate(" + hourRotation + "deg)",
                "-o-transform": "rotate(" + hourRotation + "deg)"
            });
            $("#big").css({
                "-webkit-transform": "rotate(" + minuteRotation + "deg)",
                "-moz-transform": "rotate(" + minuteRotation + "deg)",
                "-o-transform": "rotate(" + minuteRotation + "deg)"
            });
        };
        function clockStart(){
            setInterval(setTime(),1000);
            setTime();
        }
        clockStart();
    });

I'd like to understand this method and how to use it. The examples I could find looked so obvious, but still I can't make it work.

解决方案

You are making a function call in setInterval().

What you want to do is :

setInterval(setTime,1000);

这篇关于setInterval()用于模拟时钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆