每日倒数计时器/开始时间不同的计时器

时间:2018-09-26 19:49:10

标签: javascript timer countdown

我正在尝试针对特定时间段建立每日倒计时。示例:每天计数到下午3点,从下午3点到下午5点显示一条消息,再次开始倒数计时直到下午3点。为此,我在这里已经找到了几乎完美的解决方案。它每天都在选择的时间进行计数。但是现在我需要在特定日期使用不同的结束时间,例如:

周五-周二计数至下午3点 周三-周四计数至下午5点

到目前为止的代码如下:

var countDown = (function() {
                var startStream;
                var endStream;
                var streamingText = 'Streaming';
                var updateElement;

                // Pad single digit numbers
                function pad(n) {
                    return (n < 10 ? '0' : '') + +n;
                }

                // Format a time difference as hh:mm:ss
                // d0 and d1 are date objects, d0 < d1
                function timeDiff(d0, d1) {
                    var diff = d1 - d0;
                    return pad(diff / 3.6e6 | 0) + ':' + pad((diff % 3.6e6) / 6e4 | 0) + ':' + pad(diff % 6e4 / 1000 | 0);
                }

                // start, end are UNIX UTC time values in seconds for the start and end of streaming
                return function(elementId, start, end) {
                    var now = new Date();
                    var returnValue;

                    // By default, run again just after next full second
                    var delay = 1020 - now.getMilliseconds();

                    // turn start and end times into local Date objects
                    if (start) startStream = new Date(start * 1000);
                    if (end) endStream = new Date(end * 1000);

                    // If now is after endStream, add 1 day,
                    // Use UTC to avoid daylight saving adjustments
                    if (now > endStream) {
                        endStream.setUTCHours(endStream.getUTCHours() + 24);
                        startStream.setUTCHours(startStream.getUTCHours() + 24);
                    }

                    // Store the element to write the text to
                    if (elementId) updateElement = document.getElementById(elementId);

                    // If it's streaming time, return streaming text
                    if (now >= startStream && now < endStream) {
                        returnValue = streamingText;

                        // Run again after streaming end time
                        delay = endStream - now;

                    } else {
                        // Otherwise, count down to startStream
                        returnValue = timeDiff(now, startStream);
                    }

                    // Write the time left or streaming text
                    updateElement.innerHTML = returnValue;

                    // Call again when appropriate
                    setTimeout(countDown, delay);
                };
            }());


            // Create dates for a local time of xx:xx today
            var myStart = new Date();
            myStart.setHours(3, 0, 0, 0);
            var myEnd = new Date()
            myEnd.setHours(5, 0, 0, 0);

            // Create UNIX time values for same time as UTC
            var startUTCTimeValue = myStart / 1000 | 0
            var endUTCTimeValue = myEnd / 1000 | 0

            // Run when page loads
            window.onload = function() {
                countDown('foo', startUTCTimeValue, endUTCTimeValue);
            }

0 个答案:

没有答案