in Education by
I am working on a personal project and in this personal project, I am trying to implement a countdown timer. However, it seems that this is impossible with Node and an ejs file type. If anyone knows how to include a countdown timer into an ejs file, any help would be nice. I would like to know the basics so that way I can implement this into other projects if need be. Please note that I am using node js, express, MySQL, and passport for the project. Thank you for your time and patience in advance! JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can get the current timestamp and add it the countdown time. Then, you can get the remaining time via substraction with the timestamp. The current timestamp is returned by the Date.now() method in Javascript. Note that the timestamp is obtained in milliseconds. Beware on rendering. For example, if I want a ten minutes countdown: // Ten minutes to seconds to milliseconds const countdownTime = 10 * 60 * 1000; // The countdown end instant is calculed by the addition of the current time when timer is created. const expireTime = Date.now() + countdownTime; ... // Now, if you want to calculate the remaining time (in milliseconds): remainingTime = expireTime - Date.now(); // If you want to know if countdown has reached its end: isEnded = Date.now() < expireTime; ... // I don't know how do you want to implement the UI for showing the countdown stuff, but I recommend to convert the time in milliseconds to h, m, s only on browser at render time. // Here is an implementation that I made for a pomodoro application: function msToHHMMSS(timeInMs) { const timeInSeconds = timeInMs / 1000; const s = timeInSeconds % 60; const m = Math.floor(timeInSeconds / 60) % 60; const h = Math.floor(timeInSeconds / 3600); // You can use the returned values separately where do you want return [h, m, s]; }; // Use array inferencing for extracting values from the returned array const [hours, minutes, seconds] = msToHHMMSS(remainingTime); // For example, if I want to use an element with the "timer" ID for rendering the countdown time on browser: document.getElementById("timer").innerText(`${hours}:${minutes}:${seconds}`) // if you want to show the hms always with 2 digits each, you can use padStart after converting to string: document.getElementById("timer").innerText(`${hours.toString().padStart(2, "0")}: ...and so on` // The render should be upgraded once on a second. You can use the setInterval for that. Run code snippetExpand snippet Code before rendering can be executed both: on the node server or on browser. You can render it using a .js browser file (check your public folder, you can configure it on express, so this folder will be targetted by EJS for getting the path) or on a script tag on your .ejs template (code written on ejs's <% and %> stuff is executed on server before sending the page to the client, but code of js external files and on script tag is executed as client-side javascript). I hope this helped you. Carry on!

Related questions

0 votes
    Is there a way of adding custom assertions to the NodeUnit test object that gets passed to each test? ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I'm trying to include actual date and time to the certain message, however I'm unable to do it, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I'm trying to debug my nodejs app using node-inspector. But Google Chrome doesn't show the code. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
0 votes
    My websocket application accepting connections like this: wsServer.on('request', function(request) { console.log( ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    app.route('/users') .post(user.post) .get(user.get) .get(user.everyone) .put(user.update) . ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    Quick Summary of my question: Does Gulp 3.6.0 dest() handle glob-base the same way as 4.0.0? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    There are a lot of samples on running Async/Await functions. But only with one await call. I don' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 11, 2022 in Education by JackTerrance
0 votes
    I have this: On every second, all images move to the same random position But I want this: On ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have a PHP blade file and I have a list of components working as they but can't seem to pass ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am trying to add an if condition; Idea is If I enter the input as print or text with 4 in ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am trying to add an if condition; Idea is If I enter the input as print or text with 4 in ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Tell me how to make it so that when you click on a button from a cycle with page numbers, this ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am trying to add an if condition; Idea is If I enter the input as print or text with 4 in ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am trying to write some stuff in a file using reactJS and nodeJs but it doesnt work. Here is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am trying to create a simple node.js app on heroku. Here is app.js: console.log("Starting App" ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
...