Using javascript to run clearInterval on a setInterval

Hi everyone :wave:

I have near-zero javascript experience, so I’m sure the solution to my problem is very simple…

I’m running the following js function on page load, which is working as expected:

function incrementValue() {
  variables['e3a994ed-43a6-4fba-b225-46f369236104']++;
  if (variables['e3a994ed-43a6-4fba-b225-46f369236104'] === 7) {
    variables['e3a994ed-43a6-4fba-b225-46f369236104'] = 1;
  }
}

let intervalId = setInterval(incrementValue, 4000);

I would like to cancel the recurring loop above when a button on the page is clicked. The current code for that button is:

clearInterval(intervalId);

Unfortunately, this button isn’t working.

I also tried to add to the page’s body script custom code based on ChatGPT guidance:

let intervalId;

Any guidance on what I am doing wrong?

The best approach is to define a weweb variable and store your id here. Then it will be accessible for your workflow for cleaning :slight_smile:

Thanks @aurelie, and apologies for the slow response.

Just for completeness and in case anyone reads this in the future, I needed to change the following line:

let intervalId = setInterval(incrementValue, 4000);

To:

intervalId = setInterval(incrementValue, 4000);