Javascript 一天中某个时间的火灾事件

Javascript 一天中某个时间的火灾事件,javascript,jquery,Javascript,Jquery,使用javascript(jQuery解决方案也很好)是否可以在一天中的特定时间触发事件/调用函数,例如: 10:00打电话给myFunctionA 14:00打电话给myFunctionB 等等 谢谢 标记 获取当前时间 以毫秒为单位,下一次执行时间减去当前时间之间的时间差 设置超时,结果为毫秒 4html: current date: <span id="cd"></span><br /> time to Alarm: <span id="al1"&

使用javascript(jQuery解决方案也很好)是否可以在一天中的特定时间触发事件/调用函数,例如:

10:00打电话给myFunctionA

14:00打电话给myFunctionB 等等

谢谢

标记
  • 获取当前时间
  • 以毫秒为单位,下一次执行时间减去当前时间之间的时间差
  • 设置超时,结果为毫秒
  • 4html:

    current date: <span id="cd"></span><br />
    time to Alarm: <span id="al1"></span><br />
    alarm Triggered: <span id="al1stat"> false</span><br />
    

    示例位于:

    我遇到了这个问题,并提出了一个更详细的解决方案。基本思想与其他答案中的相同:使用
    setTimeout()
    ,从现在到所需时间点的间隔

    此处使用的其他详细信息:如果时间点在过去,则将其安排在明天(时间点+24小时)。从现在起,每天使用
    setInterval()
    在给定的时间点触发函数。 注:此处不考虑DST

    输入参数:
    time='16:00'
    triggerThis=function(){alert('这是被触发的');}

      scheduleWarning(time, triggerThis) {
    
        // get hour and minute from hour:minute param received, ex.: '16:00'
        const hour = Number(time.split(':')[0]);
        const minute = Number(time.split(':')[1]);
    
        // create a Date object at the desired timepoint
        const startTime = new Date(); startTime.setHours(hour, minute);
        const now = new Date();
    
        // increase timepoint by 24 hours if in the past
        if (startTime.getTime() < now.getTime()) {
          startTime.setHours(startTime.getHours() + 24);
        }
    
        // get the interval in ms from now to the timepoint when to trigger the alarm
        const firstTriggerAfterMs = startTime.getTime() - now.getTime();
    
        // trigger the function triggerThis() at the timepoint
        // create setInterval when the timepoint is reached to trigger it every day at this timepoint
        setTimeout(function(){
          triggerThis();
          setInterval(triggerThis, 24 * 60 * 60 * 1000);
        }, firstTriggerAfterMs);
    
      }
    
    scheduleWarning(时间,触发此){
    //从小时获取小时和分钟:收到分钟参数,例如:“16:00”
    常数小时=数字(时间分割(':')[0]);
    const minute=Number(time.split(“:”)[1]);
    //在所需的时间点创建日期对象
    const startTime=new Date();startTime.setHours(小时,分钟);
    const now=新日期();
    //如果在过去,则将时间点增加24小时
    if(startTime.getTime()
    事件是自动触发还是仅触发到特定的时间点?重复:好主意,尽管它没有考虑夏令时的变化(在这种情况下可能不是问题)。此解决方案的问题是,如果计算机在超时期间休眠,则报警将在很长时间后触发。为了避免从休眠状态恢复时出现问题,请让事件回调(或回调的某种包装函数)在触发时检查当前时间,因此,如果差异超过预期,它可以决定不运行。这是一个糟糕的解决方案。你在浪费无用的测试(每秒钟!?!)。只需计算时间差(您已经计算过了),并将时间间隔设置为该差。这可以在npm上找到吗?这只是一个简单的帮助函数,只需复制并集成到代码中,然后根据需要进行修改。享受吧!
     /**
                 * This Method executes a function certain time of the day
                 * @param {type} time of execution in ms
                 * @param {type} func function to execute
                 * @returns {Boolean} true if the time is valid false if not
                 */
                function executeAt(time, func){
                    var currentTime = new Date().getTime();
                    if(currentTime>time){
                        console.error("Time is in the Past");
                        return false;
                    }
                    setTimeout(func, time-currentTime);
                    return true;
                }
    
                $(document).ready(function() {
                    executeAt(new Date().setTime(new Date().getTime()+2000), function(){alert("IT WORKS");});
                });
    
      scheduleWarning(time, triggerThis) {
    
        // get hour and minute from hour:minute param received, ex.: '16:00'
        const hour = Number(time.split(':')[0]);
        const minute = Number(time.split(':')[1]);
    
        // create a Date object at the desired timepoint
        const startTime = new Date(); startTime.setHours(hour, minute);
        const now = new Date();
    
        // increase timepoint by 24 hours if in the past
        if (startTime.getTime() < now.getTime()) {
          startTime.setHours(startTime.getHours() + 24);
        }
    
        // get the interval in ms from now to the timepoint when to trigger the alarm
        const firstTriggerAfterMs = startTime.getTime() - now.getTime();
    
        // trigger the function triggerThis() at the timepoint
        // create setInterval when the timepoint is reached to trigger it every day at this timepoint
        setTimeout(function(){
          triggerThis();
          setInterval(triggerThis, 24 * 60 * 60 * 1000);
        }, firstTriggerAfterMs);
    
      }