Skip to content
Advertisement

Restarting an endless while loop upon a mouse event?

I am aiming a simple code which looks like this:

JavaScript

My goal here is to restart the print sequence from “hey 1” immediately when left mouse is clicked. However, I am not able to achieve this without using “if” conditions before every sleep. a “goto” is not working for me across different functions too. Can anyone suggest an optimal solution for this one please ?

Advertisement

Answer

You could achieve this with some kind of state machine: a function which has an internal static state to know whick was the last hey printed

JavaScript

EDIT

one problem in your loop is that you will wait a long time before reading new value from fd.

You can use signal to achieve this:

  • continously reading input from mice, without sleeping
  • when left click is detected, arm an alarm signal to display event:

Thus, your code could be:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement