[Vuejs]-How to make debounce in js

1๐Ÿ‘

โœ…

Do it naive way.

const ELAPSE_TIME = 3000; // 3 second
let oldCommand;
let lastTime;

function update() {
   if (buttonIsPressed) {
       const command = getCurrentCommand();

       if (command !== oldCommand) { // command has changed
           lastTime = new Date().getTime();
       } else { // command has not changed
           let now = new Date().getTime();
           let duration = now - lastTime;

           if (duration > ELAPSE_TIME) { // already 3 second
               postUpdate();
               lastTime = now - ELAPSE_TIME;
           }
       }
   }
}


setInterval(update, 100);
๐Ÿ‘คDaniel Tran

Leave a comment