L
L
Lion Golden2018-02-13 11:45:01
JavaScript
Lion Golden, 2018-02-13 11:45:01

How to find out how much time is left before the timer expires?

var timer = setTimeout(alert('Hello world!'), 32*60*1000);

setInterval(function(){
  var timeout = ;//нужно получить время
  console.log('Timeout через: '+timeout);
}, 1000)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2018-02-13
@nimayoleynik

It is possible like this:

var duration = 32 * 60 * 1000;
var step = 1000;
var endsAfter = duration;

var timer = setTimeout(alert, duration, 'Hello world!');

var interval = setInterval(function() {
  endsAfter -= step;
  if (endsAfter <= 0) clearInterval(interval);
  console.log('Timeout через: ', endsAfter);
}, step);

Demo

D
Dasha Tsiklauri, 2018-02-13
@dasha_programmist

In fact, no way, because setTimeout adds a task to macroTaskQueue and this does not mean that it will be executed every second. Find out the approximate time - as written above: when setting setTimeout, remember (time stamp + timeout value) and subtract the current timestamp from setInterval from it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question