Can I see if a timer is still running?

Asked
Active3 hr before
Viewed126 times

3 Answers

timerrunning
90%

Meta Stack Overflow ,Thanks for contributing an answer to Stack Overflow!, Stack Overflow Public questions & answers , Stack Overflow help chat

What I do is:

var timer = null;

if (timer != null) {
   window.clearTimeout(timer);
   timer = null;
} else {
   timer = window.setTimeout(yourFunction, 0);
}
load more v
88%

Invoke cancel on the timer. You can do this from anywhere in the program, such as from a timer task's run method. , When you run the example, you first see this: , This simple program illustrates the basic parts of implementing and scheduling a task to be executed by a timer thread. , Invoke the System.exit method, which makes the entire program (and all its threads) exit.

import java.util.Timer;
import java.util.TimerTask;

/**
 * Simple demo that uses java.util.Timer to schedule a task 
 * to execute once 5 seconds have passed.
 */

public class Reminder {
   Timer timer;

   public Reminder(int seconds) {
      timer = new Timer();
      timer.schedule(new RemindTask(), seconds * 1000);
   }

   class RemindTask extends TimerTask {
      public void run() {
         System.out.println("Time's up!");
         timer.cancel(); //Terminate the timer thread
      }
   }

   public static void main(String args[]) {
      new Reminder(5);
      System.out.println("Task scheduled.");
   }
}
load more v
72%

Search Answer Titles

if ($Timer.time_left > 0):
   print("Timer is active")
else :
   print("Timer is no longer active")

Other "timer-running" queries related to "Can I see if a timer is still running?"