• Introducing setTimeout for the hour display makes the code hard to understand. Why do you want to keep the WHILE loop that is making things complicated?

  • @maze1980, my belief is that @barry_b_benson desires to have the display illuminated for the entire duration the button is held down and not just a simple timeout after a button press as your simplicity implies. Yes it is blocking, but what else in this case would want to interrupt? As you suggest, If the LEDs are turned on, then a unique new event (another button press perhaps) will be required to turn them off. I'll defer to his response for confirmation.

    Maybe this is what you had in mind??   Untested

    var intervalID = {};
    function ct() { clearTimeout( intervalID ); }
    // Easier to type ct() than entire line
    
     
    var delay = 5000;    // msec  Turn off in case button remains stuck down
    var isButtonDown = false;  // Not used here but might be needed elsewhere
    
    function startTimer() {
    
      isButtonDown = true;
      
      hoursDisplay();    // Turn on as code inside func() occurs after timeout
    
      intervalID = setTimeout(function () {
    
        clearDisplay();
      
      }, delay);
    }
    
    function stopTimer() {
      isButtonDown = false;
      ct();
      clearDisplay();
    }
    
    function clearDisplay() {
      display(1, clear);
      display(2, clear);
    }
    
    function hoursDisplay() {
      display(1, hourFirstDigit);
      display(2, hourSecondDigit);
    }
    
    
    setWatch(function(e) {
      stopTimer();
    }, select, {repeat: true, debounce: 50, edge: "rising"});
    
About

Avatar for Robin @Robin started