Program Club

타이머가 아직 실행 중인지 확인할 수 있습니까?

proclub 2020. 11. 27. 21:39
반응형

타이머가 아직 실행 중인지 확인할 수 있습니까?


여기에 대한 답을 찾을 수없는 간단한 질문 : a setTimeout가 설정 되면 여전히 설정되었는지 확인할 수있는 방법이 있습니까?

if (!Timer)
{
    Timer = setTimeout(DoThis,60000);
}

내가 말할 수 있듯이, clearTimeout변수는 마지막 값으로 유지됩니다. A console.log방금 Timer시간 제한이 설정되었거나 해제되었는지 여부에 관계없이 를 '12'로 보았습니다 . 변수를 null로 설정하거나 다른 변수를 부울로 사용하여 예,이 타이머를 설정해야합니까? 확실히 시간 초과가 아직 실행 중인지 확인하는 방법이 있습니다. 아직 실행중인 경우 남은 시간을 알 필요가 없습니다.


타이머를 시작하거나 중지하는 것 외에는 어쨌든 타이머와 상호 작용할 수 없습니다. 일반적으로 타이머가 실행되고 있지 않음을 나타내는 플래그를 사용하는 대신 타임 아웃 처리기에서 타이머 변수를 null로 설정합니다. 타이머 작동 방식 에 대한 W3Schools 에 대한 멋진 설명이 있습니다 . 그들의 예에서는 플래그 변수를 사용합니다.

보고있는 값 은 현재 타이머에 대한 핸들 이며이를 지울 때 (중지) 사용됩니다.


내가하는 일은 :

var timer = null;

if (timer != null) {
  window.clearTimeout(timer); 
  timer = null;
}
else {
  timer = window.setTimeout(yourFunction, 0);
}

타이머 clearTimeout를 시작하기 전에 실행 하기 만하면 기존 타이머를 확인할 필요가 없습니다 .

var timer;
//..
var startTimer = function() {
  clearTimeout(timer);
  timer = setTimeout(DoThis, 6000);
}

이렇게하면 새 인스턴스를 시작하기 전에 모든 타이머가 지워집니다.


Timer_Started = true타이머로 다른 변수 설정 하십시오. 또한 false타이머 함수가 호출 될 때 변수를 변경합니다 .

// set 'Timer_Started' when you setTimeout
var Timer_Started = true;
var Timer = setTimeout(DoThis,60000);

function DoThis(){

   // function DoThis actions 
   //note that timer is done.
   Timer_Started = false;

}

function Check_If_My_Timer_Is_Done(){

   if(Timer_Started){
      alert("The timer must still be running.");
   }else{
      alert("The timer is DONE.");
   }

}

나는 이것이 necroposting이라는 것을 알고 있지만 여전히 사람들이 이것을 찾고 있다고 생각합니다.

이것이 내가 사용하는 것입니다 : 3 개의 변수 :

  1. t 이후 밀리 초 동안 .. 다음 대상의 날짜 개체에서
  2. timerSys 실제 간격 동안
  3. seconds 밀리 초에 대한 임계 값이 설정되었습니다.

난이 옆 function timer변수 인 경우 1 개 변수 함수 검사 진정 타이머가 이미 실행 중이면 그 검사 그렇다면,이 칠보다 케이스 인 경우 글로벌 바르 그렇지 않으면 , 거짓 , 간격 및 세트 클리어 global var timerSys오류를 ;

var t, timerSys, seconds;

function timer(s) {
  if (s && typeof s === "number") {
    if (typeof timerSys === "boolean" || typeof timerSys === "undefined") {
      timerSys = setInterval(function() {
        sys();
      }, s);
      t = new Date().setMilliseconds(s);
      seconds = s;
    }
  } else {
    clearInterval(timerSys);
    timerSys = false;
  }
  return ((!timerSys) ? "0" : t)
}

function sys() {
  t = new Date().setMilliseconds(seconds);

}

예 I

이제 sys 함수에 줄을 추가 할 수 있습니다 .

function sys() {
  t = new Date().setMilliseconds(seconds);
  console.log("Next execution: " + new Date(t));
//this is also the place where you put functions & code needed to happen when interval is triggerd

}

그리고 실행하십시오 :

  timer(5000);

콘솔에서 5 초마다 :

  //output:: Next execution: Sun May 08 2016 11:01:05 GMT+0200 (Romance (zomertijd))

예 II

function sys() {
  t = new Date().setMilliseconds(seconds);
  console.log("Next execution: " + seconds/1000 + " seconds");

}

$(function() {
  timer(5000);
});

콘솔에서 5 초마다 :

      //output:: Next execution: 5 seconds

예 III

var t, timerSys, seconds;

function timer(s) {
  if (s && typeof s === "number") {
    if (typeof timerSys === "boolean" || typeof timerSys === "undefined") {
      timerSys = setInterval(function() {
        sys();
      }, s);
      t = new Date().setMilliseconds(s);
      seconds = s;
    }
  } else {
    clearInterval(timerSys);
    timerSys = false;
  }
  return ((!timerSys) ? "0" : t)
}

function sys() {
  t = new Date().setMilliseconds(seconds);
  console.log("Next execution: " + seconds / 1000 + " seconds");

}

$(function() {
  timer(5000);

  $("button").on("click", function() {
    $("span").text(t - new Date());
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Freebeer</button>
<span></span>

이 방법으로 0 아래로 갈 수 있습니다.


나는 보통 타이머를 무효화합니다.

var alarm = setTimeout(wakeUpOneHourLater, 3600000);
function wakeUpOneHourLater() {
    alarm = null;    //stop alarm after sleeping for exactly one hour
}
//...
if (!alarm) {
    console.log('Oops, waked up too early...Zzz...');
}
else {
    console.log('Slept for at least one hour!');
}

참고URL : https://stackoverflow.com/questions/3247173/can-i-see-if-a-timer-is-still-running

반응형