Stop Current Coroutine, Then Restart It

Trying to trigger a coroutine loop at random intervals, but before it starts, it needs to interrupt and exit the previous instance of it, so only one instance of the loop is running at a time:

while (x == true)
{
     Do_Action_Over_A_Number_Of_Frames();
     yield;
}

Tried a bunch of different methods with no luck. Can’t use StopCoroutine() since I have multiple instances of it running on other game objects. I can turn off the previous loop by making x=false but then the new loop won’t start. I could have x=false followed by x=true but then the first loop will never exit and the second loop will stack.

I could disable and enable the script that contains the above coroutine but that seems like overkill.

I feel like I’m missing something really simple but scoured the Forums and Answers with no luck.

Came up with something - it works but I’m definitely open to suggestions.

This function is called every time an object gets hit - it needs to interrupt the previous instance of the loop and start a new one.

The first line in the function turns off the previous instance of the loop, then turns back on the bool so it can run a new instance:

function Action()
{
     x = false; yield; x = true;
     while (x == true)
     {
          Do_Action_Over_A_Number_Of_Frames();
          yield;
     }
}