Javascript: pause setTimeout();

Asked
Active3 hr before
Viewed126 times

6 Answers

pausesettimeoutjavascript
90%

If you mean setInterval then no, you can't pause it, you can only cancel it (clearInterval) and then re-schedule it again. Details of all of these in the Timers section of the spec.,Is there anyway to pause and resume it? ,If you have several divs to hide, you could use an setInterval and a number of cycles to do like in:,I don't think you'll find anything better than clearTimeout. Anyway, you can always schedule another timeout later, instead 'resuming' it.

You could wrap window.setTimeout like this, which I think is similar to what you were suggesting in the question:

var Timer = function(callback, delay) {
   var timerId, start, remaining = delay;

   this.pause = function() {
      window.clearTimeout(timerId);
      remaining -= Date.now() - start;
   };

   this.resume = function() {
      start = Date.now();
      window.clearTimeout(timerId);
      timerId = window.setTimeout(callback, remaining);
   };

   this.resume();
};

var timer = new Timer(function() {
   alert("Done!");
}, 1000);

timer.pause();
// Do some stuff...
timer.resume();
load more v
88%

If you’d like to find out more about JavaScript’s setTimeout method, please consult our tutorial which has plenty of examples to get you going.,Sleep in JavaScript – delay between actions,The standard way of creating a delay in JavaScript is to use its setTimeout method. For example:,What is the JavaScript version of sleep()?

Consider the following Ruby code:

require 'net/http'
require 'json'

url = 'https://api.github.com/users/jameshibbard'
uri = URI(url)
response = JSON.parse(Net::HTTP.get(uri))
puts response['public_repos']
puts "Hello!"
load more v
72%

When you pass a method to setTimeout(), it will be invoked with a this value that may differ from your expectation. The general issue is explained in detail in the JavaScript reference. , In WebExtensions, setTimeout() does not work reliably. Extension authors should use the alarms API instead. , setTimeout() is an asynchronous function, meaning that the timer function will not pause execution of other functions in the functions stack. In other words, you cannot use setTimeout() to create a "pause" before the next function in the function stack fires. ,© 2005-2021 Mozilla and individual contributors. Content is available under these licenses.

var timeoutID = setTimeout(function [, delay, arg1, arg2, ...]);
var timeoutID = setTimeout(function [, delay]);
var timeoutID = setTimeout(code[, delay]);
load more v
65%

But one of the things which JavaScript misses is a way to "pause" execution for a while and resume it later. In this post, I'll discuss how you can achieve that and what it really means to "pause" or "sleep" in JavaScript. ,One of those features are Promises, which are probably the most widely used feature in JavaScript after ES5 was released.,You came here for a simple sleep instruction in JavaScript, and ended up learning about one of the core things in JavaScript - event loops! Amazing, isn't it? ,Unlike other languages, JavaScript doesn't just keep on executing code in a linear fashion from top to bottom. It is an asynchronous event-driven language with tons of magic in the form of the event loop.

Here's the copy-pasta code which does the job:

/**
 * 
 * @param duration Enter duration in seconds
 */
function sleep(duration) {
   return new Promise(resolve => {
      setTimeout(() => {
         resolve()
      }, duration * 1000)
   })
}
load more v
75%

/echo simulates Async calls:
JSON: /echo/json/
JSONP: //jsfiddle.net/echo/jsonp/
HTML: /echo/html/
XML: /echo/xml/

/echo
load more v
40%

this.pause = function () { this.clear(); delay -= new Date() - start; };, this.resume = function () { start = new Date(); timer = setTimeout(function () { callback.apply(self, Array.prototype.slice.call(args, 2, args.length)); }, delay); };, this.pause = function() { window.clearTimeout(timerId); remaining -= new Date() - start; };, this.resume = function() { start = new Date(); window.clearTimeout(timerId); timerId = window.setTimeout(callback, remaining); };

function Timer(callback, delay) {
   var timerId, start, remaining = delay;

   this.pause = function() {
      window.clearTimeout(timerId);
      remaining -= new Date() - start;
   };

   this.resume = function() {
      start = new Date();
      window.clearTimeout(timerId);
      timerId = window.setTimeout(callback, remaining);
   };

   this.resume();
}

var timer = new Timer(function() {
   alert("Done!");
}, 1000);

timer.pause(); // Do some stuff...timer.resume();
load more v

Other "pause-settimeout" queries related to "Javascript: pause setTimeout();"