-4

我有以下倒数计时器代码。我需要将 JavaScript 部分转换为 jQuery。倒数计时器在页面加载时启动。我该怎么做,因为我必须在加载页面时加载 diffTime 函数。请帮我。提前致谢。

编辑:我知道 jquery 调用无法从 ** function CreateTimer() ** 访问 ** function Tick() ** 。jQuery 中有没有 ** setTimeout() ** 的库?据我所知,它是 JS 原生的。

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript">
        var Timer;
        var TotalSeconds;


        function CreateTimer(TimerID, Time) {
            Timer = document.getElementById(TimerID);
            TotalSeconds = Time;
            //UpdateTimer()
            window.setTimeout("Tick()", 1000);
        }

        function Tick() {
            if (TotalSeconds <= 0) {
                //alert("Time's up!")
                document.getElementById("timeMsg").innerHTML = "Market closed!! ";
                return;
            }

            TotalSeconds -= 1;
            UpdateTimer()
            window.setTimeout("Tick()", 1000);
        }

        function UpdateTimer() {
            var Seconds = TotalSeconds;

            var Days = Math.floor(Seconds / 86400);
            Seconds -= Days * 86400;

            var Hours = Math.floor(Seconds / 3600);
            Seconds -= Hours * (3600);

            var Minutes = Math.floor(Seconds / 60);
            Seconds -= Minutes * (60);


            var TimeStr = ((Days > 0) ? Days + " days " : "") + LeadingZero(Hours) + ":" + LeadingZero(Minutes) + ":" + LeadingZero(Seconds)


            Timer.innerHTML = TimeStr;
        }



        function LeadingZero(Time) {

            return (Time < 10) ? "0" + Time : + Time;

        }

        function diffTime(){

            var startTime = 10*60 + 30; //starting time in minute
            var lastTime = 16*60 + 30;  //ending time in minutes

            var thisTime = new Date();              // now
            var currentYear = thisTime.getFullYear();        
            var currentMonth = thisTime.getMonth();
            var currentDay = thisTime.getUTCDate();
            var currentHour = thisTime.getHours();
            var currentMinute = thisTime.getMinutes();

            var currentTime = currentHour*60 + currentMinute;   //current time in minute

            if(currentTime >= startTime && currentTime < lastTime){

                var endTime = new Date(currentYear,currentMonth,currentDay,16,30);  // 4:30pm           

                var diff = endTime.getTime() - thisTime.getTime();   // now
                var remainTime = diff / (1000);     // positive number of days
                remainTime = Math.ceil(remainTime);
                CreateTimer("timer", remainTime);


            }else{
                document.getElementById("timeMsg").innerHTML = "Market closed!! ";
                document.getElementById("timer").innerHTML = "00:00:00";

            }

        }
        window.onload = diffTime;
    </script>


</head>
<body>

    <div><span id="timeMsg">Elapsed time remain: </span><b><span id='timer'></span></b></div>


</body>

4

2 回答 2

0

在 document.ready() 中调用函数

 $( document ).ready( function ()
    {

diffTime();
});
于 2013-03-25T07:00:07.803 回答
0

如果您希望它在整个(即图像、对象等)页面加载后启动:

$(window).load(function() {}

否则,您可以在 HTML 已加载且 DOM 准备就绪时启动计时器:

$(document).ready(function() {}
于 2013-03-25T07:06:52.513 回答