【问题标题】:Fire event at a certain time of the day一天中特定时间的火灾事件
【发布时间】:2011-07-28 08:20:39
【问题描述】:

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

在 10:00 致电 myFunctionA

在 14:00 致电 myFunctionB 等等。

谢谢

标记

【问题讨论】:

标签: javascript jquery


【解决方案1】:
  • 获取当前时间
  • 以毫秒为单位获取下一次执行时间减去当前时间的时间差
  • 设置超时,结果为毫秒

【讨论】:

  • 好主意,虽然它没有考虑夏令时变化(在这种情况下可能不是问题)。
  • 这个解决方案的问题是,如果计算机在超时期间休​​眠,那么警报会在很晚的时候触发
  • 为了避免从睡眠中恢复时的问题,让事件回调(或回调的某种包装函数)检查它触发时的当前时间,以便它可以决定不运行如果差异超出预期。
【解决方案2】:

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

此处使用的其他详细信息:如果时间点在过去,则安排在明天(时间点 + 24 小时)。从现在起每天使用setInterval() 在给定时间点触发该功能。 注意:这里不考虑夏令时。

输入参数:
time = '16:00';
triggerThis = function() { alert('this was triggered'); };

  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);

  }

【讨论】:

  • 这可以在 npm 上找到吗?
  • 它只是一个简单的辅助函数,只需将其复制并集成到您的代码中,并根据需要进行修改。享受吧!
【解决方案3】:
 /**
             * 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");});
            });

【讨论】:

    【解决方案4】:

    4html:

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

    javascript:

    var setAlarm1 = "14"; //14:00, 2:00 PM
    var int1 = setInterval(function(){
        var currentHour = new Date().getHours();
        var currentMin = new Date().getMinutes();
        $('#cd').html(currentHour + ":" + currentMin );
        $('#al1').html(setAlarm1 - currentHour + " hours");
            if(currentHour >= setAlarm1){
                $('#al1stat').html(" true");
                clearInterval(int1);
                //call to function to trigger : triggerFunction();
            }
        },1000) //check time on 1s
    

    样本地址:http://jsfiddle.net/yhDVx/4/

    【讨论】:

    • 这是一个糟糕的解决方案。你在浪费无用的测试(每秒钟!?!)。只需计算时间差(您已经计算过了),然后将间隔设置为该时间差。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多