Go back Hotwire / Stimulus / Turbo ...

delay execution of code in JS - setTimer


setTimer is a global function sets a timer which piece of code once the timer expires (this is NOT a debounce)

This is used when yo want to delay execution before another piece of code get executed
setTimeout(() => {
  alert("you will see me in 3 sec")
}, 3000)

For example Dropzone JS has a feature to check if file is valid after file was added but that check takes a while before it gets reflected in file.accepted  variable

  dropzone.on("addedfile", file => {
      setTimeout(() => {
        if (file.accepted) {
          // uploadd
        }
      }
   }