在一天中的某个时间发生火灾事件

时间:2022-12-03 22:36:56

Is it possible with javascript (jQuery solutions are fine too) to fire an event/call a function at certain times of the day e.g.

使用javascript (jQuery解决方案也不错)是否可能在一天中的某些时候触发事件/调用函数?

call myFunctionA at 10:00

叫myFunctionA 10点

call myFunctionB at 14:00 etc..

在14:00打电话给myFunctionB等。

Thanks

谢谢

Mark

马克

4 个解决方案

#1


16  

  • Get current time
  • 获取当前时间
  • Get in milliseconds, the time difference between next execution time minus current time
  • 以毫秒为单位,即下一次执行时间减去当前时间的时间差。
  • Settimeout with result millisecons
  • Settimeout和结果millisecons

#2


1  

 /**
             * 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");});
            });

#3


0  

4html:

html:4

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

javascript:

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

Sample at: http://jsfiddle.net/yhDVx/4/

示例:http://jsfiddle.net/yhDVx/4/

#4


0  

I ran into this problem and came up with a more elaborate solution. The basic idea is the same as in the other answers: use setTimeout() with an interval from now to the desired time point.

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

Other details used here: if the time point is in the past, schedule it for tomorrow (at time point + 24 hours). Trigger the function at the given time point every day from now using setInterval(). Note: DST is not considered here.

这里使用的其他细节:如果时间点是过去的,把它安排在明天(时间点+ 24小时)。从现在开始,每天使用setInterval()在给定的时间点触发函数。注意:这里不考虑DST。

Input params:
time = '16:00';
triggerThis = function() { alert('this was triggered'); };

输入参数: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);

  }

#1


16  

  • Get current time
  • 获取当前时间
  • Get in milliseconds, the time difference between next execution time minus current time
  • 以毫秒为单位,即下一次执行时间减去当前时间的时间差。
  • Settimeout with result millisecons
  • Settimeout和结果millisecons

#2


1  

 /**
             * 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");});
            });

#3


0  

4html:

html:4

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

javascript:

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

Sample at: http://jsfiddle.net/yhDVx/4/

示例:http://jsfiddle.net/yhDVx/4/

#4


0  

I ran into this problem and came up with a more elaborate solution. The basic idea is the same as in the other answers: use setTimeout() with an interval from now to the desired time point.

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

Other details used here: if the time point is in the past, schedule it for tomorrow (at time point + 24 hours). Trigger the function at the given time point every day from now using setInterval(). Note: DST is not considered here.

这里使用的其他细节:如果时间点是过去的,把它安排在明天(时间点+ 24小时)。从现在开始,每天使用setInterval()在给定的时间点触发函数。注意:这里不考虑DST。

Input params:
time = '16:00';
triggerThis = function() { alert('this was triggered'); };

输入参数: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);

  }