N
N
nikit-nikit2021-06-19 23:41:52
JavaScript
nikit-nikit, 2021-06-19 23:41:52

Countdown timer with notification. How to do?

Hello!
How can I make it so that after the end of the timer a message is displayed that the time is up?

Here's the timer: https://jsfiddle.net/alexander_js_developer/Lk5v9ncw/
Here's an example message output: https://yournet.info/example/timer/timer-v1.html

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Efrosinium Ostroff, 2021-06-20
@nikit-nikit

To display a message, it is better to write a separate function.
For example:

const end = () => {
  alert('end');
}

Now it remains to track when we need to call this function. In the code you provided, there is already an interval in which the countdown occurs. It is called every second. It already provides an end event when the countdown stops (clearInterval). Otherwise it would be -1, -2, -3, ...
The step() function would look like this:
const step = () => {
  if( state.timeleft.seconds-1 < 0 ){
    if( state.timeleft.minutes !== 0 ){
      state.timeleft.seconds = 59;
      state.timeleft.minutes = ( state.timeleft.minutes-1 < 0)  ? 0 : state.timeleft.minutes-1;
    } else {
    	clearInterval( state.timerId );
        end();
    }
  } else {
    state.timeleft.seconds--;
  }
  render();
}

We called end() at the place where clearInterval() occurs. Therefore, the message will be called after 00:00 on the timer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question