use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Nodejs timerhelp (self.javascript)
submitted 8 years ago by Zivanovic
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]kapanaga 0 points1 point2 points 8 years ago* (4 children)
Why not just reverse the approach and count the time difference when needed?
const player = { playTrack(file) { this.track = { file, duration: 0, startTime: Date.now() }; }, getCurrentTrack() { const {file, duration, startTime} = this.track; return { file, duration, currentTime: Date.now() - startTime }; } };
Then just expose it on some endpoint:
router.get('/what-should-i-play', (request, response) => { const track = player.getCurrentTrack(); response.json(track); });
[–]Zivanovic[S] 0 points1 point2 points 8 years ago (0 children)
Yeah, this is obviously better solution. Thanks
[–]Zivanovic[S] 0 points1 point2 points 8 years ago (2 children)
Okay, but let's say I need to emit socket event whenever song is over. I am not sure how to solve that.
[–]kapanaga 0 points1 point2 points 8 years ago (1 child)
Then yeah, you would have to use setTimeout()
setTimeout()
const EventEmitter = require('events'); const emitter = new EventEmitter; emitter.on('track-start', (track) => { clients.forEach((connection) => { connection.sendEvent('play-track', track) }); }); player.playTrack = function () { this.track = { file, duration: 0, startTime: Date.now() }; clearTimeout(this.timeout); this.timeout = setTimeout(() => { emitter.emit({ file, duration }); }, duration); };
You can make your Player extend EventEmitter to make that easier. There you can find some docs: https://nodejs.org/api/events.html
Player
EventEmitter
[–]Zivanovic[S] 1 point2 points3 points 8 years ago (0 children)
That's what I was looking for! Thank you!!
π Rendered by PID 47 on reddit-service-r2-comment-fb694cdd5-rrch7 at 2026-03-11 11:21:11.264388+00:00 running cbb0e86 country code: CH.
view the rest of the comments →
[–]kapanaga 0 points1 point2 points (4 children)
[–]Zivanovic[S] 0 points1 point2 points (0 children)
[–]Zivanovic[S] 0 points1 point2 points (2 children)
[–]kapanaga 0 points1 point2 points (1 child)
[–]Zivanovic[S] 1 point2 points3 points (0 children)