在fadeOut中未调用setInterval

setInterval does not get invoked in fadeOut

本文关键字:调用 setInterval fadeOut      更新时间:2023-09-26

是什么原因导致setInterval未在fadeOut中调用?

<!doctype html>
<html>
<head>
    <title>Exercise</title>
    <style>
        #box {
        }
    </style>
</head>
<body>
    <div id="box"></div>
    <script src="jquery.js"></script>
    <script>
        $(function () {
            $("#box").fadeOut(3000, function () {
                setInterval("UpdateTime()", 100);
            });
        });
        function GetTime() {
            var now = new Date();
            var obj = {
                Hour: now.getHours(),
                Minute: now.getMinutes(),
                Second: now.getSeconds()
            };
            return obj;
        }
        function UpdateTime() {
            var box = $("#box");
            var obj = GetTime();
            if (obj.Hour < 10)
                obj.Hour = "0" + obj.Hour;
            if (obj.Minute < 10)
                obj.Minute = "0" + obj.Minute;
            if (obj.Second < 10)
                obj.Second = "0" + obj.Second;
            box.text(obj.Hour + ":" + obj.Minute + ":" + obj.Second);
        }
    </script>
</body>
</html>

setInterval()设置为之前建议的家伙:

$("#box").fadeIn(3000, function () {
    setInterval(UpdateTime, 100);
});

并且(如果您想延迟时间)将.fadeOut()更改为.fadeIn()(或完全消除衰落)。

Fiddle:http://jsfiddle.net/cu3s5zwt/

避免在setInterval中使用字符串。更喜欢传递函数。

你可以这样做:

setInterval(UpdateTime, 100);

或者如果你需要一些论据:

setInterval(function(){
    UpdateTime(argA, argB);
}, 100);