Javascript Moment.js在特定时间创建弹出窗口

Javascript Moment.js在特定时间创建弹出窗口,javascript,jquery,html,momentjs,Javascript,Jquery,Html,Momentjs,这是我第一次使用moment.js,但我正在尝试创建一个弹出窗口或按钮出现的时间段。因为这是我第一次使用它,所以文档中不清楚如何进行: 我的想法是通过持续时间部分或操作 $('.button').hide(); if(moment.duration(9, 'hours')){ $('.button').show(); } else{ $('.button').hide(); } 类似这样的事情,但我不确定,我正在尝试获取它,以便时间应该从上午9点开始,无需使用持续时间。moment.js不提供

这是我第一次使用moment.js,但我正在尝试创建一个弹出窗口或按钮出现的时间段。因为这是我第一次使用它,所以文档中不清楚如何进行: 我的想法是通过持续时间部分或操作

$('.button').hide();
if(moment.duration(9, 'hours')){
$('.button').show();
}
else{
$('.button').hide();
}

类似这样的事情,但我不确定,我正在尝试获取它,以便时间应该从上午9点开始,无需使用持续时间。moment.js不提供超时时间,持续时间只是时间上的持续时间,如2小时等,所以只需使用普通的一个:

function showSomething(){
   $('#something').show();
}

setTimeout(showSomething, 1000);
你当然也可以写:

setTimeout(showSomething, moment.duration(2, 'seconds'));

但是我看不到您问题中的用例。

不需要使用持续时间。moment.js不提供超时时间,持续时间只是时间上的持续时间,如2小时等,所以只需使用普通的一个:

function showSomething(){
   $('#something').show();
}

setTimeout(showSomething, 1000);
你当然也可以写:

setTimeout(showSomething, moment.duration(2, 'seconds'));

但是我看不到您问题中的用例。

要在特定时间显示按钮,例如11:12小时,您可以尝试以下方法:

$('.button').hide();

function showButton(){
   $('.button').show();
};

function refreshAt(hours, minutes, seconds) {
   var now = new Date();
   var then = new Date();

   if(now.getHours() > hours || (now.getHours() == hours && now.getMinutes() > minutes) || now.getHours() == hours && now.getMinutes() == minutes && now.getSeconds() >=  seconds) {
           then.setDate(now.getDate() + 1);
   }
   then.setHours(hours);
   then.setMinutes(minutes);
   then.setSeconds(seconds);

   var timeout = (then.getTime() - now.getTime());
   setTimeout(showButton, timeout);
};

refreshAt(11, 12, 00);

要在特定时间显示按钮,例如11:12小时,您可以尝试以下操作:

$('.button').hide();

function showButton(){
   $('.button').show();
};

function refreshAt(hours, minutes, seconds) {
   var now = new Date();
   var then = new Date();

   if(now.getHours() > hours || (now.getHours() == hours && now.getMinutes() > minutes) || now.getHours() == hours && now.getMinutes() == minutes && now.getSeconds() >=  seconds) {
           then.setDate(now.getDate() + 1);
   }
   then.setHours(hours);
   then.setMinutes(minutes);
   then.setSeconds(seconds);

   var timeout = (then.getTime() - now.getTime());
   setTimeout(showButton, timeout);
};

refreshAt(11, 12, 00);

我最终在网站外使用了get set函数,因此它看起来如下所示:

$('.button').hide();
if(moment().hours() >= 9 && moment().hours() <= 11){
   $('.button').show();
}
else{
   $('.button').hide();
}

它在momentjs中运行得很好

我在网站外使用了get set函数,因此它看起来如下所示:

$('.button').hide();
if(moment().hours() >= 9 && moment().hours() <= 11){
   $('.button').show();
}
else{
   $('.button').hide();
}

在momentjs中效果很好

我成功地将上述两种方法结合起来。我需要在特定的时间间隔内激活一个按钮:归功于Chridam和jsg。我的解决方案是:

    $('#myButton').hide();
        if(moment().hours() >= 13&& moment().hours() <= 17)
          {
            $('#myButton').show();
           }
      else if (moment().hours() >= 1 && moment().hours() <= 2)
{
   $('#myButton').show();
}
else{
   $('#myButton').hide();
}


    // Ability to refresh
    function refreshAt(hours, minutes, seconds) {
   var now = new Date();
   var then = new Date();

   if(now.getHours() > hours || (now.getHours() == hours && now.getMinutes() > minutes) || now.getHours() == hours && now.getMinutes() == minutes && now.getSeconds() >=  seconds) {
           then.setDate(now.getDate() + 1);
   }
   then.setHours(hours);
   then.setMinutes(minutes);
   then.setSeconds(seconds);

   //var timeout = (then.getTime() - now.getTime());
   //setTimeout(showButton, timeout);
};
    refreshAt(13, 00, 00);
    refreshAt(17, 0, 00);
    refreshAt(1, 00, 00);
    refreshAt(2, 00, 00);

我设法将上述两种方法结合起来。我需要在特定的时间间隔内激活一个按钮:归功于Chridam和jsg。我的解决方案是:

    $('#myButton').hide();
        if(moment().hours() >= 13&& moment().hours() <= 17)
          {
            $('#myButton').show();
           }
      else if (moment().hours() >= 1 && moment().hours() <= 2)
{
   $('#myButton').show();
}
else{
   $('#myButton').hide();
}


    // Ability to refresh
    function refreshAt(hours, minutes, seconds) {
   var now = new Date();
   var then = new Date();

   if(now.getHours() > hours || (now.getHours() == hours && now.getMinutes() > minutes) || now.getHours() == hours && now.getMinutes() == minutes && now.getSeconds() >=  seconds) {
           then.setDate(now.getDate() + 1);
   }
   then.setHours(hours);
   then.setMinutes(minutes);
   then.setSeconds(seconds);

   //var timeout = (then.getTime() - now.getTime());
   //setTimeout(showButton, timeout);
};
    refreshAt(13, 00, 00);
    refreshAt(17, 0, 00);
    refreshAt(1, 00, 00);
    refreshAt(2, 00, 00);