Как создать функцию, которая выполняется, если время равно x? - PullRequest
0 голосов
/ 22 января 2019

Я создавал веб-страницу и хотел бы получить помощь в этом. Мне нужен всплывающий текст, когда UTCHours и UTCMinutes равны определенным значениям. Я хочу что-то подобное. Это похоже на модель, а не на реальный код.

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

Ответы [ 3 ]

0 голосов
/ 22 января 2019
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
   }
}
0 голосов
/ 22 января 2019

Конечно, я не сделаю все для вас, но вот, пожалуйста. База для начала.

// 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

0 голосов
/ 22 января 2019

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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...