【问题标题】:How do I create a function that executes if the time is equal to x?如果时间等于 x,如何创建一个执行的函数?
【发布时间】:2019-06-16 06:30:39
【问题描述】:

我正在创建一个网页,我希望获得这方面的帮助。当 UTCHours 和 UTCMinutes 等于特定值时,我需要弹出一个文本。我想要这样的东西。这就像模型,而不是实际代码。

h = utcTime
m = utcminutes
if (h=x and m=y) 
   {
    function myFunction();
}

【问题讨论】:

  • 获取页面加载的当前时间,执行数学运算以获取直到所需时间的持续时间,为该持续时间设置超时以执行该方法。
  • 关于下面的答案,setTimeout()setInterval() 更合适。如果您计算从现在到它应该运行的时间之间的持续时间,则没有理由每隔一段时间再检查一次。您知道他们需要等待方法执行多长时间。运行间隔会产生不必要的开销。

标签: javascript html date time


【解决方案1】:
setInterval(function(){
now = new Date();
hours = now.getUTCHours();
mins = now.getUTCMinutes();
executeFunctionIfEqualToDefined(hours, mins);
},60000); // this block will run every 60000 milli seconds i.e 60 seconds =1 min


function executeFunctionIfEqualToDefined(hours, mins){
  if(hours === x && mins === y){
    //execute your code here
   }
}

【讨论】:

  • Downvoters plz 添加 cmets 为什么?就像我们把时间花在解决方案和产生答案上,但只是有人投反对票
  • 你的帖子刚刚出现在Low Quality Postsreview queue。造成这种情况的原因(以及可能有人对您的答案投反对票的原因)是您的帖子包含的代码没有解释它是如何工作的和/或它如何解决 OPs 问题。
  • 它也有语法和逻辑错误,两个注释语句之一是错误的。
  • 我确实编辑了我的答案,希望这次我将评论声明标记为正确。感谢通知
【解决方案2】:

当然,我不会为你做所有事情,但你去吧。一个开始的基地。

// The button functionality
const $ = (x) => document.getElementById(x);

$('start').addEventListener('click', ()=>{
    startTime();
});

$('stop').addEventListener('click', ()=>{
    stopTime();
});

// The timer

// set variables
let date,
start,
stop;

function startTime() {
  date = new Date(); // get current date.
  start = date.getTime(); // get time from current date
  // Just output
  document.getElementById("showStart").innerHTML = start;
}

function stopTime() {
  date = new Date(); // get current date.
  stop = date.getTime(); // get time from current date
  // just output
  document.getElementById("showStop").innerHTML = stop;
  document.getElementById("difference").innerHTML = stop-start;
}

jsfiddle

我很无聊,所以你去吧。

// The button functionality
const $ = (x) => document.getElementById(x);

$('setAlert').addEventListener('click', ()=>{
    setAlert(3); // alert in x seconds.
});

// set variables
let date,
target,
stop,
interval = 1; // integer.

function setAlert(time) {
  date = new Date(); // get current date.
  target = date.getTime() + ( (time * 1000) - ((interval * 1000) +1000) ); // sets the time it should alert.
  /*
   * time -1 because it's a 1s interval, that means when it reaches x = y it will alert 1s later by the next "check". This why you need to subtract 1s.
   * (time-1)*1000 is because the getTime returns time in ms not in seconds. You would need to make it setAlert(3000),
   *         i made it bit easier by multiplying the value by 1000 = ms.
   */
  // The loop that checks time
  setInterval(function(){ // This function makes the "checking each X ms"
      if(stop !== target){ // Check if time was reached.
          stopTime(); // Check time
      }else{
          alert("TIME IS OVER"); // When time is reached do this.
      }
  }, interval*1000); // Refreshes the time each second.

  // Just output
  document.getElementById("showTarget").innerHTML = target+(interval*1000); // Because the target time has "minus" in it (line 16) it needs to be added to show the real target time.
}


function stopTime() {
  date = new Date(); // get current date.
  stop = date.getTime(); // get time from current date
  // just output
  document.getElementById("showStop").innerHTML = stop;
  // document.getElementById("difference").innerHTML = stop-target;
}

jsfiddle v2

【讨论】:

  • 而不是setInterval,为什么不直接计算出所需时间的间隔并用setTimeout设置一个间隔呢?当然,这一切都取决于本地主机,但也许这并不重要……比如:delay = new Date().setUTCHours(h, m, 0, 0) - new Date()。 ;-)
  • 这可以通过不同的方式完成,我的网站也需要类似的东西,我会做不同的。更高效:)
【解决方案3】:

function displayAlert(hour,minute){

   //create a new Date Object(Current Date and Time)
   var date = new Date();
   
   // getUTCHours getUTCMinutes are inbuilt methods 
   // for getting UTC hour and minute by comparing it with timezone
   var utcHour = date.getUTCHours();
   var utcMinutes = date.getUTCMinutes();
   
   //display alert when utc hour and minute matches sired time
   if(utcHour == hour && utcMinutes == minute){
        alert(utcHour + " : " + utcMinutes)
   }
}

displayAlert(18,31)

【讨论】:

  • 您可以将此代码添加到 setInterval 中以连续检查它
猜你喜欢
  • 1970-01-01
  • 2019-04-08
  • 2016-12-29
  • 1970-01-01
  • 2020-12-20
  • 1970-01-01
  • 2018-09-08
  • 1970-01-01
  • 2021-12-03
相关资源
最近更新 更多