How do I rerun function without overlap instances in javascript?

So I want to rerun my function with updated values based on an event function.

Because i’m trying to use this javascript as a content-script, while statements don’t work as they just spam outputs and crash the page.

No if statements as it will just go through the code before the event and not repeat the if statement after I trigger the event. Here is basically what I’m trying to do.

function outer(value) {
  function inner() {
     outer(value); //rerun the function
     //then cancel this instance of the function
  
  }
  window.addEventListener('keydown', function(event) {
  //call function based off of evennt
  inner()
  }
}

I’ve tried all the ways to exit functions, but things like setInterval count as functions, so if I have a if statement be checked in intervals, it will only exit the setInterval.