0
Students
This practice includes elements of asynchronous programming (setTimeout) since bind is mainly used in this context
timer.js
Implement and export as default a function that returns a timer object. The timer is set for a specific duration and launches. Every 100 milliseconds, it invokes a callback, passing two parameters: state
with the working
value and elapsedTime
that contains the amount of time that has passed since the timer began (in milliseconds). When the timer is over, it calls the same callback with the state
parameter and the finished
value.
// Callback
const cb = ({ state, elapsedTime }) => {
switch (state) {
case 'working':
console.log(`Time elapsed: ${elapsedTime}`);
break;
case 'finished':
console.log(`Timer has finished!`);
}
};
// Creates timer object
const timer = makeTimer(300, cb); // Set it up for 300 milliseconds
timer.start();
// Time elapsed: 100
// Time elapsed: 200
// Time elapsed: 300
// Timer has finished!
Tips
- Timers
- Use setInterval to start the timer
- Use clearInterval to refresh it
- Each new tick should not affect other ticks in any way. The data must be isolated
For full access to the challenge you need a professional subscription.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.