jQuery 计时器仅 30 分钟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13328919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:31:12  来源:igfitidea点击:

Timer for only 30 mins

jquerytimer

提问by Rakesh Shetty

I want to set a timer for example my product should be display for only 30 mins when it is added to cart. I tried :-

我想设置一个计时器,例如我的产品在添加到购物车时应该只显示 30 分钟。我试过 :-

var d = new Date();

var minute = d.getHours()+30;

Also I want to show user how much time is remaining (countdown)

另外我想向用户展示剩余的时间(倒计时)

I know this is wrong. I want to do this using jQuery. I am new to jquery please help me on this

我知道这是错误的。我想使用 jQuery 来做到这一点。我是 jquery 的新手,请帮助我

Thanks in advance.

提前致谢。

回答by wf9a5m75

yeah this somewhat correct but I want to show user how much time is remaining, i.e countdown should start.How to do that

是的,这有点正确,但我想向用户展示剩余的时间,即倒计时应该开始。如何做到这一点

Ok, how about this:

好的,这个怎么样:

var countdown = 30 * 60 * 1000;
var timerId = setInterval(function(){
  countdown -= 1000;
  var min = Math.floor(countdown / (60 * 1000));
  //var sec = Math.floor(countdown - (min * 60 * 1000));  // wrong
  var sec = Math.floor((countdown - (min * 60 * 1000)) / 1000);  //correct

  if (countdown <= 0) {
     alert("30 min!");
     clearInterval(timerId);
     //doSomething();
  } else {
     $("#countTime").html(min + " : " + sec);
  }

}, 1000); //1000ms. = 1sec.

Or I recommend you use these plugins: http://www.tripwiremagazine.com/2012/09/jquery-countdown-scripts.html

或者我建议您使用这些插件:http: //www.tripwiremagazine.com/2012/09/jquery-countdown-scripts.html

回答by Indigenuity

You could take a look at javascript timers here. If you need to time for 30 minutes, try

您可以在此处查看 javascript 计时器。如果您需要计时 30 分钟,请尝试

var timer = window.setInterval(function()
{
    //doSomething
}, 1800000);

EDIT

编辑

To keep it from going off again later, use window.clearInterval(timer);

为了防止它以后再次熄灭,请使用 window.clearInterval(timer);

EDIT

编辑

If you want, you could keep track of how much time has passed, rather than have one big timer.

如果您愿意,您可以跟踪已经过去了多少时间,而不是拥有一个大计时器。

var startTime = (new Date()).getTime();
var secondsLeft;
var timer = window.setInterval(function()
{
    var now = (new Date()).getTime();
    secondsLeft = 1800 - ((now - startTime) / 1000);
    //do something here to display secondsLeft
}

回答by wf9a5m75

If you want to do it in Javascript, you can do it like this:

如果你想用 Javascript 来做,你可以这样做:

var waitTime = 30 * 60 * 1000; // = 30min.

setTimeout(function(){
  alert("30 min!");
}, waitTime);

And if you want to use jQuery, you can use jQuery Timer plugin. http://jquery.offput.ca/every/

如果你想使用 jQuery,你可以使用 jQuery Timer 插件。 http://jquery.offput.ca/every/

But I think if you use PHP and the system is shopping cart(or something), maybe you should do it using session mechanism of PHP.

但是我认为如果您使用PHP并且系统是购物车(或其他东西),也许您应该使用PHP的会话机制来做。