Vue,js Watch Input Changes With Delay

May 3, 2021
<template>  <div>    <input v-model="query" />  </div></template><script>export default {  data() {    return {      query: null    }  },  watch: {    query(val) {      if (this.timer) {        clearTimeout(this.timer)      }      let sleep = 300      switch(this.query.length) {        case 1:          sleep = 1000          break        case 2:        case 3:          sleep = 700          break        case 4:        case 5:          sleep = 500      }      this.timer = setTimeout(() => {        console.log(this.query)      }, 300)    }  },}  </script>

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.