0

enter image description hereI have this set up to start a countdown timer...

const initialCountdown = 10; // Initial countdown value (change as needed)
let countdown = initialCountdown;

const intervalId = setInterval(() => {
  if (countdown > 0) {
    countdown--;
    setSeconds(countdown);
  } else {`your text`
    resetInterval(intervalId);
    countdown = initialCountdown;
    setSeconds(countdown);
  }
}, 1000);

function resetInterval(intervalId) {
  clearInterval(intervalId);
  // Perform any other reset operations here if needed
}

This starts the timer and resets it at 0

I need to stop it glitching when the start button is pressed twice and I need to add a stop/resume function to another button. I use draftbit which is a lowcode app app builder which allows custom code. i use an on press action for the button to start the timer (see attached). Ne to javaScript as you can probably tell ... Please help

I have tried several versions of the code and everything i have tried that is not the above totally breaks the timer

1
  • To add an image, click edit then the icon that looks like a mountain with a moon Commented Jun 8, 2023 at 13:33

1 Answer 1

0

Just test if it is running

const initialCountdown = 10; // Initial countdown value (change as needed)
let countdown = initialCountdown;
let intervalId;

function onPressHandler() {
  if (intervalId) return;
  let intervalId = setInterval(() => {
    if (countdown > 0) {
      countdown--;
      setSeconds(countdown);
    } else {
      resetInterval(intervalId);
      countdown = initialCountdown;
      setSeconds(countdown);
    }
  }, 1000);
}

function resetInterval(intervalId) {
  clearInterval(intervalId);
  // Perform any other reset operations here if needed
}
Sign up to request clarification or add additional context in comments.

10 Comments

Hi, thanks for your help! this didn't run unfortunately
In what way? Error message? I can only guess since you do not have a proper JS implementation
when i press the on press trigger which is connected to the code you gave me ... the countdown doesn't start... again thanks for your help
Can you show how you assign the handler? I called it onPressHandler expecting you to know how to use it
yes .. sorry iim very new to this ... this being the action when the trigger/button is pressed?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.