倒计时器与饼干 [英] Countdown timer with cookies

查看:80
本文介绍了倒计时器与饼干的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多这样的话题,但我只是遇到了无法找到答案的问题。
我的脚本是:

I know there have been a lot of topics like this but I just have problem to which I couldn't find the answer. My script is:

window.onload = function(){
    // 200 seconds countdown
    var countdown = 14400; 
    //current timestamp
    var now   = Date.parse(new Date());

    //ready should be stored in your cookie
    if ( !document.cookie )
    {
        document.cookie = Date.parse(new Date (now + countdown  * 1000)); // * 1000 to get ms
    }

    //every 1000 ms
    setInterval(function()
    {
        var diff = ( document.cookie - Date.parse(new Date()) );

        if ( diff > 0 )
        {
            var message = diff/1000 + " seconds left";
        }
        else
        {
            var message = "finished";
        }

        document.body.innerHTML = message;

    },1000);
}

我想制作倒数计时器,告诉用户时间取决于他的剩余多少饼干价值。到目前为止,我设法计算两个值之间的差异,但我不知道如何制作格式,例如,从差异时间戳(差异),dd / mm / yy hh:mm:ss。有可能吗?

I want to make countdown timer which tells user time how much left depending on his cookie value. So far I managed to calculate difference between two values but I don't know how to make format like, let's say, "dd/mm/yy hh:mm:ss" from difference timestamp (diff). Is it possible at all?

推荐答案

你想要的是一个将(mili)秒的差异转换为类似

What you want is a function that converts difference in (mili)seconds to something like

5d 4h 3m 2s

5d 4h 3m 2s

如果您不介意在几个月的时间内有大量的日子,那么您可以使用类似这样的事情:

If you don't mind having a large number of days for times periods > a few months, then you could use something like this:

function human_time_difference(diff) {
    var s = diff % 60; diff = Math.floor(diff / 60);
    var min = diff % 60; diff = Math.floor(diff / 60);
    var hr = diff % 24; diff = Math.floor(diff / 24);
    var days = diff;

    return days + 'd ' + hr + 'h ' + min + 'm ' + s + 's';
}

如果你有毫秒的差异,你需要通过数字除以1000.您也可以使用Math.round去除分数,但如果您希望显示该信息,也可以将它们保持打开状态。

If you have the difference in miliseconds, you'll need to pass the that number divided by 1000. You can also use Math.round to get rid of fractions, but you could just as well leave them on if you want that information displayed.

获取由于以下几个原因,数月和数年有点棘手:

Getting months and years is a little trickier for a couple of reasons:


  1. 一个月内的天数各不相同。

  2. 当你从一个月的中间到下一个月的中间时,时间跨度不会涵盖整个月,即使天数> 31(例如,有多少个月之间) 6月2日和7月30日??)。

如果你真的想要两次之间的月数,这个数字它们之间的秒数是不够的。您必须使用日历逻辑,这需要传递开始日期和结束日期+时间。

If you really want the number of months between two times, the number of seconds between them is not enough. You have to use calendar logic, which requires passing in the start and end date + time.

PS:当您发布问题时,请避免不相关的详细信息。例如,您的问题与cookie,setInterval或onload处理程序无关。你不知道的唯一部分是如何将(mili)秒转换为天,小时等。提供一些关于你为什么要做某事的背景可能会有所帮助,但是如果不是必须要理解基本问题,把它放在最后,以便人们在进入必要部分之前不必趟过它。同样的建议也适用于你的头衔;通过排除不相关的细节(例如cookie和倒计时)来确保它是相关的。

PS: When you post a question, avoid irrelevant details. For example, your question has nothing to do with cookies, setInterval, or onload handlers. The only part that you don't know is how to convert (mili)seconds to days, hours, etc. It might be helpful to supply some background on why you're trying to do something, but if it's not essential to understand the basic question, put it at the end so that people don't have to wade through it before getting to the essential part. The same advice applies to your title; make sure it's relevant by excluding irrelevant details (e.g. cookies and counting down).

这篇关于倒计时器与饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆