简体   繁体   English

localStorage如何保存倒计时值,以便在我重新加载页面时不会重置

[英]localStorage how to save countdown value so it wont reset when I reload the page

When I reload the page, my 1 minute countdown also reloads. 重新加载页面时,我的1分钟倒计时也会重新加载。 I tried to use localStorage but it seems to me failed. 我尝试使用localStorage但对我来说似乎失败了。 Please have a look, I do not know where I should fix. 请看看,我不知道应该在哪里修复。

Thank you 谢谢

My script 我的剧本

        /* for countdown */
    var countDown = (function ($) {
        // Length ms
        var timeOut = 10000;
        // Interval ms
        var timeGap = 1000;

        var currentTime = (new Date()).getTime();
        var endTime = (new Date()).getTime() + timeOut;

        var guiTimer = $("#clock");
        var running = true;
        var timeOutAlert = $("#timeout-alert");
        timeOutAlert.hide();

        var updateTimer = function() {
            // Run till timeout
            if(currentTime + timeGap < endTime) {
                setTimeout( updateTimer, timeGap );
            }
            // Countdown if running
            if(running) {
                currentTime += timeGap;
                if(currentTime >= endTime) { // if its over
                    guiTimer.css("color","red");
                }
            }
            // Update Gui
            var time = new Date();
            time.setTime(endTime - currentTime);
            var minutes = time.getMinutes();
            var seconds = time.getSeconds();

            guiTimer.html((minutes < 10 ? '0' : '') + minutes + ':' + (seconds < 10 ? '0' : '') + seconds);

            if (parseInt(guiTimer.html().substr(3)) <= 10){ // alert the user that he is running out of time
                guiTimer.css('color','red');
                timeOutAlert.show();
            }

        };

        var pause = function() {
            running = false;
        };

        var resume = function() {
            running = true;
        };

        var start = function(timeout) {
            timeOut = timeout;
            currentTime = (new Date()).getTime();
            endTime = (new Date()).getTime() + timeOut;
            updateTimer();
        };

        return {
            pause: pause,
            resume: resume,
            start: start
        };
    })(jQuery);

    jQuery('#break').on('click',countDown.pause);
    jQuery('#continue').on('click',countDown.resume);

    var seconds = 60; // seconds we want to count down
    countDown.start(seconds*1000);

I tried to fix it but I dont know where/how to put localStorage. 我试图修复它,但是我不知道在哪里/如何放置localStorage。

This may help you. 这可能对您有帮助。

HTML Code: HTML代码:

<div id="divCounter"></div>

JS Code JS代码

var test = 60;
if (localStorage.getItem("counter")) {
    if (localStorage.getItem("counter") <= 0) {
        var value = test;
        alert(value);
    } else {
        var value = localStorage.getItem("counter");
    }
} else {
    var value = test;
}
document.getElementById('divCounter').innerHTML = value;

var counter = function() {
    if (value <= 0) {
        localStorage.setItem("counter", test);
        value = test;
    } else {
        value = parseInt(value) - 1;
        localStorage.setItem("counter", value);
    }
    document.getElementById('divCounter').innerHTML = value;
};

var interval = setInterval(function() { counter(); }, 1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 重新载入页面时,倒数计时器会重设 - Countdown timer gets reset when I reload the page 当我重新加载页面时,倒计时重新加载到 - When i reload the page, the countdown reload to 如何使我的页面可以关闭或重新加载,但当我再次打开页面时内容不会消失? - How do I make it so my page can close or reload but the content wont disappear when i open the page again? JavaScript:本地存储更改时如何重新加载页面? - JavaScript: How to reload page when localstorage changes? 如何为倒数计时器创建会话存储,以便在重新加载页面后不重置它? - How can I create a session storage for my countdown timer so it's not reset after reloading the page? 倒数结束时,如何使计时器重新载入页面? - how can i make my timer to reload to page when the countdown ends? 如何将此页面保存到 localStorage - How can I save this page to localStorage 在 LocalStorage 中重新加载页面时,如何使删除图标不更改为添加图标? - How to make the remove icon not change to the add icon when I reload the page in LocalStorage? 为什么刷新页面时localStorage条目会重置? - Why do localStorage entries get reset when I refresh the page? 如果我重新加载页面,倒计时将重新开始-如何设置修复 - Countdown is starting anew if I reload the page - how to set fix
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM