【问题标题】:How Do I Create A Countdown Time That Resets At A Specific Time如何创建在特定时间重置的倒计时时间
【发布时间】:2012-07-18 18:47:13
【问题描述】:

首先让我说我对 javascript 没有深入的了解,但我知道如何以足够的方式为页面编写小脚本。我的一个客户需要我为网站执行以下操作:

  1. 在用户的计算机上查找用户的本地时间。
  2. 以当地时间减去下午 6 点。
  3. 在倒计时中显示该时间,或仅在声明中显示当天发货的剩余时间。
  4. 下午 6 点后时间会重置或消失,直到下一个工作日。

到目前为止,我已经能够创建从本地计算机获取时间的逻辑。我以为我可以使用 datejs,但它不会计算一天的小时数。

这是我当前的代码:

var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()

var suffix = "AM";
if (hours >= 12) 
{
  suffix = "PM";
  hours = hours - 12;
 }

var suffix = "AM";

if (hours >= 12) 
{
  suffix = "PM"; 
  hours = hours - 12;
}

if (hours == 0) 
{
  hours = 12;
}

if (minutes < 10)
  minutes = "0" + minutes;

document.write("<b>" + hours + ":" + minutes + " " + suffix + "</b>");

【问题讨论】:

  • 除了您正在进行的任何其他测试外,请记住测试夏令时日期,否则 x hours 输出可能会延迟 +/- 1 小时

标签: javascript datetime time timer


【解决方案1】:

这个怎么样:

var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()

var suffix = "AM";
if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
  }


if (minutes < 10)
minutes = "0" + minutes

if (suffix == "PM" && hours >= 6)
    {
 document.write("You're too late for next day shipping!");
}
    else
    {
    var hoursLeft = 5 - hours;
var minsLeft = 60 - minutes;
    document.write("<b> You've got " + hoursLeft  + " hours and " + minsLeft + " minutes left to qualify for next day shipping! </b>")
    }

【讨论】:

  • 这假设客户在同一时区,这不是一个安全的假设。如何处理 javascript 中的时区偏移量...我不确定。
  • @Giovanni B: 你必须得到客户位置(地理位置),然后选择正确的时区。
  • 完美!非常感谢 GiovanniB
【解决方案2】:

如果这个网站允许我评论其他人的答案,我会将这归功于 Giovanni,但由于我还不能评论其他人的工作,所以这里需要更改。

var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()

var suffix = "AM";

if (minutes < 10)
minutes = "0" + minutes

if (hours >= 18)
{
 document.write("You're too late for next day shipping!");
}
else
{
var hoursLeft = 17 - hours;
var minsLeft = 60 - minutes;
if(minsLeft==60){
    minsLeft=0;
    hoursLeft++;
}

document.write("<b> You've got " + hoursLeft  + " and " + minsLeft + " minutes left to qualify for next day shipping! </b>")
}

原因是早上 5 点下单的人可能会认为他们必须在下一小时内提交才能在第二天发货,而实际上他们还有 13 小时。

编辑:看到了您的时区问题,here 是您可能感兴趣的帖子。

编辑 2: 发布了错误的链接。正确的答案现在应该出来了,尽管它可能有点过时了。

【讨论】:

  • Scott 我刚刚测试了你的代码,当我改变我的时间时它工作了。再次感谢您和 Giovanni 的帮助。
  • 如果 currentTime 是 new Date(2012, 6, 18, 0, 0, 0, 0),输出会显示 You've got 17 and 60 minutes left 而不是 18 小时。
  • @andyb 已编辑以解决该问题。谢谢。
  • @NinaMorena 我在 if 检查中犯了一个愚蠢的错误,只使用了= 而不是==。它不应该再出现这个问题了。
  • 不,我的信用tttttt!!!呵呵,感谢您修复我的代码。我只是在没有彻底测试的情况下发出了一个几乎存在的答案……真丢脸。很好的收获@ScottM
【解决方案3】:

我昨天也解决了类似的问题,所以这很容易。这是javascript代码:

function start_onload(last_hour){
    var timeout_message = document.getElementById('timeout_message');
    var currentTime = new Date();
    var hours = currentTime.getHours();
    var minutes = currentTime.getMinutes();
    var seconds = currentTime.getSeconds();
    var expire_time = 0; // in seconds
    if (hours<last_hour) {
        expire_time += (last_hour-hours-1)*3600;
        expire_time += (59-minutes)*60;
        expire_time += (59-seconds);
    }
    else {
        timeout_message.innerHTML = 'It\'s after '+last_hour+' o\'clock!';
        return;
    }
    var expire_time = currentTime.getTime() + 1000*expire_time;
    //console.log(expire_time, hours, minutes, seconds, expire_time);
    function countdown_session_timeout() {
        var current_time = new Date().getTime();
        var remaining = Math.floor((expire_time - current_time)/1000);
        if (remaining>0) {
            hours = Math.floor(remaining/3600);
            minutes = Math.floor((remaining - hours*3600)/60);
            seconds = remaining%60;
            timeout_message.innerHTML = 'Countdown will stop in '+ hours + ' hours ' + minutes + ' min. ' + seconds + ' sec.';
            setTimeout(countdown_session_timeout, 1000);
        } else {
            timeout_message.innerHTML = 'Time is up!';
        }
    }
    countdown_session_timeout();
}

完整的脚本@pastebin.com 在这里。

【讨论】:

    【解决方案4】:

    这是一个简单的倒计时计时器,从函数运行后的 30 秒开始,到 0 结束。到达 0 后,它会自动重置计数器。它再次进入 30 秒,并在循环中继续此过程

     window.onload = function() { startCountDown(30,
    1000,我的功能); }
    
    
    函数 startCountDown(i, p, f) { var pause = p;变量 fn = f;
    var countDownObj = document.getElementById("countDown");
    
       countDownObj.count = 函数(i){
          //写出计数
          countDownObj.innerHTML = i;
          如果(我 == 0){
          //执行函数
          //fn();
                    startCountDown(30, 1000, myFunction); //停止
          返回; } 设置超时(函数(){
          // 重复
          countDownObj.count(i - 1);
          },
          暂停
          ); } //设置它 countDownObj.count(i); }
    
    函数 myFunction(){};
    
    
        </script>
    

    【讨论】:

      猜你喜欢
      • 2015-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      相关资源
      最近更新 更多