What is Debouncing and Throttling in JavaScript?

·

2 min read

Let's take an example of a đ˜€đ—”đ—Œđ—œđ—œđ—¶đ—»đ—Ž 𝗰𝗼𝗿𝘁 web app so that there will be a đ—°đ—”đ—Čđ—°đ—žđ—Œđ˜‚đ˜ đ—Żđ˜‚đ˜đ˜đ—Œđ—» đ˜đ—Œ đ—œđ—č𝗼𝗰đ—Č đ—źđ—» đ—Œđ—żđ—±đ—Č𝗿 đ—¶đ—ł 𝗼 𝘂𝘀đ—Č𝗿 đ—źđ—°đ—°đ—¶đ—±đ—Čđ—»đ˜đ—źđ—čđ—č𝘆 𝗰đ—čđ—¶đ—°đ—žđ˜€ đ˜đ—”đ—Č đ—Żđ˜‚đ˜đ˜đ—Œđ—» đ˜đ˜„đ—¶đ—°đ—Č đ˜đ—”đ—Čđ—» đ—¶đ˜ đ˜„đ—¶đ—čđ—č đ—œđ—č𝗼𝗰đ—Č đ˜đ—”đ—Č đ—Œđ—żđ—±đ—Č𝗿 đ˜đ˜„đ—¶đ—°đ—Č? (so it depends on the design of the Web App)

𝗘𝘅𝗼đ—șđ—œđ—čđ—Č đ—–đ—Œđ—±đ—Č đ˜„đ—¶đ˜đ—”đ—Œđ˜‚đ˜ 𝗗đ—Čđ—Żđ—Œđ˜‚đ—»đ—°đ—¶đ—»đ—Ž đ—źđ—»đ—± đ—§đ—”đ—żđ—Œđ˜đ˜đ—čđ—¶đ—»đ—Ž

Click Me

document.getElementById(‘y’).addEventListener(‘click’, e => { console.log(“Event triggered”) })

Codepen: https://codepen.io/yashmantri/pen/LYpdQZ

đ—Šđ—Œ, đ—”đ—Č𝗿đ—Č 𝗗đ—Čđ—Żđ—Œđ˜‚đ—»đ—°đ—¶đ—»đ—Ž đ—źđ—»đ—± đ—§đ—”đ—żđ—Œđ˜đ˜đ—čđ—¶đ—»đ—Ž đ—°đ—Œđ—șđ—Č𝘀 đ—¶đ—»đ˜đ—Œ đ˜đ—”đ—Č đ—œđ—¶đ—°đ˜đ˜‚đ—żđ—Č. 𝗜𝘁 đ—¶đ˜€ 𝘂𝘀đ—Čđ—± đ˜đ—Œ đ—źđ˜ƒđ—Œđ—¶đ—± đ˜‚đ—»đ—»đ—Č𝗰đ—Č𝘀𝘀𝗼𝗿𝘆 đ—Č𝘃đ—Čđ—»đ˜đ˜€.

đ—™đ—¶đ—żđ˜€đ˜ đ—čđ—Č𝘁'𝘀 đ—±đ—¶đ˜€đ—°đ˜‚đ˜€đ˜€ 𝗗đ—Čđ—Żđ—Œđ˜‚đ—»đ—°đ—¶đ—»đ—Ž:

𝗗đ—Čđ—Żđ—Œđ˜‚đ—»đ—°đ—¶đ—»đ—Ž đ—șđ—Čđ—źđ—»đ˜€ that if you keep clicking on the same button the event will đ—Œđ—»đ—č𝘆 𝗯đ—Č đ˜đ—żđ—¶đ—Žđ—Žđ—Č𝗿đ—Čđ—± đ—Œđ—» đ˜đ—”đ—Č đ—č𝗼𝘀𝘁 𝗰đ—čđ—¶đ—°đ—ž.

𝗗đ—Čđ—Żđ—Œđ˜‚đ—»đ—°đ—Č đ—łđ˜‚đ—»đ—°đ˜đ—¶đ—Œđ—» 𝘁𝗼𝗾đ—Č𝘀 đ˜đ˜„đ—Œ 𝗼𝗿𝗮𝘂đ—șđ—Čđ—»đ˜đ˜€ đ—łđ—¶đ—żđ˜€đ˜ đ—Œđ—»đ—Č đ—¶đ˜€ đ˜đ—”đ—Č đ—łđ˜‚đ—»đ—°đ˜đ—¶đ—Œđ—» đ˜đ—”đ—źđ˜ đ˜†đ—Œđ˜‚ đ˜„đ—źđ—»đ˜ đ˜đ—Œ đ—Č𝘅đ—Č𝗰𝘂𝘁đ—Č đ—Œđ—» 𝗼 𝗰đ—čđ—¶đ—°đ—ž đ—źđ—»đ—± đ˜đ—”đ—Č 𝘀đ—Čđ—°đ—Œđ—»đ—± đ—Œđ—»đ—Č đ—¶đ˜€ đ˜đ—”đ—Č đ—±đ—Čđ—č𝗼𝘆.

Codepen: https://codepen.io/yashmantri/pen/jObpzZz

carbon (15).png

đ—Ąđ—Œđ˜„ đ—čđ—Č𝘁'𝘀 đ—±đ—¶đ˜€đ—°đ˜‚đ˜€đ˜€ đ—§đ—”đ—żđ—Œđ˜đ˜đ—čđ—¶đ—»đ—Ž:

đ—§đ—”đ—żđ—Œđ˜đ˜đ—čđ—¶đ—»đ—Ž đ—¶đ˜€ đ˜€đ—¶đ—șđ—¶đ—č𝗼𝗿 đ˜đ—Œ 𝗗đ—Čđ—Żđ—Œđ˜‚đ—»đ—°đ—¶đ—»đ—Ž. It avoids unnecessary events.

đ—Šđ—Œ đ˜†đ—Œđ˜‚ đ—ș𝘂𝘀𝘁 𝗯đ—Č đ˜„đ—Œđ—»đ—±đ—Čđ—żđ—¶đ—»đ—Ž đ˜„đ—”đ—źđ˜ đ—¶đ˜€ đ˜đ—”đ—Č đ—±đ—¶đ—łđ—łđ—Č𝗿đ—Čđ—»đ—°đ—Č 𝗯đ—Č𝘁𝘄đ—Čđ—Čđ—» đ˜đ—”đ—Čđ—ș?

đ—§đ—”đ—żđ—Œđ˜đ˜đ—čđ—¶đ—»đ—Ž đ—șđ—Čđ—źđ—»đ˜€ that i𝗳 đ˜†đ—Œđ˜‚ 𝗰đ—čđ—¶đ—°đ—ž 𝗼 đ—Żđ˜‚đ˜đ˜đ—Œđ—», đ˜đ—”đ—Č đ—Č𝘃đ—Čđ—»đ˜ đ˜„đ—¶đ—čđ—č 𝗯đ—Č đ˜đ—żđ—¶đ—Žđ—Žđ—Č𝗿đ—Čđ—± and đ˜„đ—¶đ˜đ—”đ—¶đ—» đ˜đ—”đ—Č đ—±đ—Čđ—č𝗼𝘆 (argument to the throttling function) if the user 𝗰đ—čđ—¶đ—°đ—žđ˜€ đ˜đ—”đ—Č 𝘀𝗼đ—șđ—Č đ—Żđ˜‚đ˜đ˜đ—Œđ—» đ—źđ—Žđ—źđ—¶đ—», the đ—Č𝘃đ—Čđ—»đ˜ đ˜„đ—¶đ—čđ—č đ—»đ—Œđ˜ 𝗯đ—Č đ˜đ—żđ—¶đ—Žđ—Žđ—Č𝗿đ—Čđ—±.

The throttling function takes two arguments first one is the function that you want to execute on a click and the second one is the delay.

Codepen: https://codepen.io/yashmantri/pen/ZEbxrBj

carbon (16).png

𝗛𝗼𝘃đ—Č đ˜†đ—Œđ˜‚ đ—Č𝘃đ—Č𝗿 𝘂𝘀đ—Čđ—± đ˜đ—”đ—żđ—Œđ˜đ˜đ—čđ—¶đ—»đ—Ž đ—Œđ—ż đ—±đ—Čđ—Żđ—Œđ˜‚đ—»đ—°đ—¶đ—»đ—Ž?

Â