简体   繁体   English

JavaScript网站-倒数计时器

[英]JavaScript Site - Countdown Timer

I am looking to create timer which starts a countdown timer. 我正在寻找创建启动倒数计时器的计时器。 The code works fine until you change the page. 该代码可以正常工作,直到您更改页面为止。 As the timer runs through the loop and starts the timer again. 当计时器运行通过循环并再次启动计时器时。

Ideally I want the timer to countdown regardless of the page on the entire domain. 理想情况下,无论整个域上的页面如何,我都希望计时器倒计时。

In addition, if $sessionDecline is clicked I want a 10 minute timer be invoked, otherwise $sessionAccept to return to the homepage with a counter of 5 minutes. 另外,如果单击$ sessionDecline,我希望调用一个10分钟的计时器,否则$ sessionAccept返回带有5分钟计数器的主页。

var SessionTime     = 2000,
    tickDuration    = 1000,
    $sessionAccept  = $('#session--accept'),
    $sessionDecline = $('#session--decline');

if ( Modernizr.localstorage && lscache.get('sessionActive') === null ){

    var myInterval  = setInterval(function(){

            SessionTime = SessionTime - tickDuration

        }, 1000),

        myTimeOut   = setTimeout(SessionExpireEvent, SessionTime);

} else if ( Modernizr.localstorage && lscache.get('sessionActive') === true ){

    SessionTime = 6000;
    var myInterval  = setInterval(function(){

            SessionTime = SessionTime - tickDuration

        }, 1000),

        myTimeOut   = setTimeout(SessionExpireEvent, SessionTime);

}

function SessionExpireEvent() {
    clearInterval(myInterval);

    $('#timout--popup').addClass('is--loaded');

    $sessionAccept.click(function(){

        window.location.href = '/';

        lscache.set('sessionActive', true);

        $(this)
            .closest('.overlay__wrapper')
            .removeClass('is--loaded');
    });

    $sessionDecline.click(function(){
        lscache.set('sessionActive', false);

        $(this)
            .closest('.overlay__wrapper')
            .removeClass('is--loaded');
    });
}

I am using: 我在用:

jQuery v1 / ls-cache / custom modernizr jQuery v1 / ls缓存 /自定义modernizr

Sorry in advance for not making a coded example. 抱歉,没有提供编码示例。

I would figure out what the date/time of the 10 or 5 minutes from now would be and write that value to a cookie which you can do in jQuery like this $.cookie("test", 1); 我会弄清楚从现在开始的10或5分钟的日期/时间是什么,然后将该值写入cookie,您可以像这样在$.cookie("test", 1);在jQuery中进行操作$.cookie("test", 1);

Then calculate the milliseconds from now until your target value and set your timeout for that many milliseconds. 然后计算从现在起直到您的目标值的毫秒数,然后将超时设置为该毫秒数。

on page load check to see if your cookie is set using $.cookie("test"); 在页面加载中检查是否使用$.cookie("test");设置了cookie $.cookie("test"); if it is set, then use that value to set your timeout, if not, then calculate your timeout and set the cookie. 如果已设置,则使用该值设置您的超时;如果未设置,则计算您的超时并设置cookie。

Also make sure to destroy the cookie in your function using $.removeCookie("test"); 还请确保使用$.removeCookie("test");破坏函数中的cookie $.removeCookie("test");

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM