在特定日期/时间打开模式

Open Modal in a specific date/time

本文关键字:模式 时间 日期      更新时间:2023-09-26

我一直在开发一个网络应用程序,我想知道是否有办法在特定日期/时间显示模型。

在应用程序中,用户可以预订任务或提醒,因此当我从数据库中读取任务时,我想安排在用户指定的日期/时间显示模态。

例如,用户在 2013-09-23 的 14:00 预订了一个任务,我想在模态中显示一条消息。

我kwon我们可以用JavaScript设置时间间隔:

setInterval(function () { 
    showModal(); 
}, 10 * 1000);

但是如何像样本中那样指定小时?

setInterval调用中,10 * 1000表示 10 乘以 1000 毫秒,换句话说是 10 秒。如果你想要一个小时,它只是一个更大的数字。 1000 * 60 * 60是一个小时。

但是,setInterval用于多次运行函数。除非您希望每小时调用一次,否则您可能正在寻找setTimeoutsetTimeout计划在时间段到期后运行一次函数。

你可以这样尝试。

  1. 让您的setInterval()持续运行一段时间。
  2. 通过将它们转换为milliseconds来比较日期。 和比较条件。

setInterval(function () { 
 var date = new Date("2013-09-23 14:00");
 var schDateSecs = Date.parse(date);
 var currentDate = new Date();
 var schCurrentSecs = Date.parse(currentDate);
 if (schDateSecs == schCurrentSecs) {
    //execute
    showModal(); 
 }
}, 10 * 1000);

谢谢大家的回答,他们帮助我想出了一个解决方案:

function setNotification(notificationDate, notificationCallback){
     var currentDate = new Date();
     var date = new Date(notificationDate);
     var interval = date - currentDate; 
    if(interval > 0)
        window.setTimeout(function(){ notificationCallback(item)}, interval); //notificationCallback = showModal()
}