setTimeout with Date object
setTimeout with Date object
I'm creating a timeout based on user input, with the input format being: 1min
or 2h
, and deciding if it's a minute or an hour via the following code;
1min
2h
if (duration.includes("h", 1))
/* If the collectedDuration includes "h" in it,
parse the string into an integer and multiply it with an hour in miliseconds */
const intDuration = parseInt(duration, 10);
const parsedDuration = intDuration * 3600000;
// Create the timer with setTimeout where parsedDuration is the delay
createTimer(item, parsedDuration);
else if (duration.includes("m", 1))
const intDuration = parseInt(duration, 10);
const parsedDuration = intDuration * 60000;
createTimer(item, parsedDuration);
What I want to do: Figure out how much time is left until setTimeout completes at any given time before it does. For example: Timer is created for 1 hour and 15 minutes later, I use a command to display the time left which would be 45 minutes.
I tried the convertion method found here but that is static; it only converts base miliseconds to hours. I need something dynamic.
I have also tried doing it with Date objects but failed. How can I go on about it?
Can you provide a snippet Minimal, Complete, and Verifiable example where we can reproduce and understand what you are trying to do?
– Calvin Nunes
Aug 31 at 17:11
^^ Help us help you
– Uzer
Aug 31 at 17:12
Apologies. This is for a Discord bot and the code is rather lenghty. This is a snippet from a code that creates a giveaway for a duration. What I want to do is, after the timer is created with
setTimeout
, use a command that displays how much time is left for the giveaway to conclude. The whole code for this command can be found on GitHub.– Penemue
Aug 31 at 17:25
setTimeout
2 Answers
2
You can't do that with vanilla setTimeout
. You'll have to wrap it:
setTimeout
class Timeout
// this is a pretty thin wrapper over setTimeout
constructor (f, n, ...args)
this._start = Date.now() + n; // when it will start
this._handle = setTimeout(f, n, ...args);
// easy cancel
cancel ()
clearTimeout(this._handle);
// projected start time - current time
get timeLeft ()
return this._start - Date.now();
I wish they'd provided an OO interface for timeouts/intervals in the first place. Usage:
const timeout = new Timeout(console.log, 2000, 'foo', 'bar');
setTimeout(() => console.log(timeout.timeLeft), 1000);
Should print something like
1000
foo bar
Over the course of a couple of seconds.
This answer is incomplete, more of a suggestion.
You said you failed attempting to do what you need using the Date object, consider whatever that method was using the momentjs library. Moment makes modifying Date considerably easier than converting to epoch then adding/subtracting ms.
https://momentjs.com/
You can do stuff like:
var x = moment(); // sets x to now
x.subtract(10, 'minute'); // subtracts 10 minutes
console.log(x.format('MMMM Do YYYY, h:mm:ss a')); // see what you have now
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Do you need a label to show how much is remaining ?
– SergioEscudero
Aug 31 at 17:10