JavaScript Interview Questions - Question 21 #debounce #functionthrottling #coding #interview #js

preview_player
Показать описание
@ByteWarpDev Presents
JavaScript Interview Questions

Question - 21:
Guest the output of the following code.

function debounce(func, delay) {
let timeoutId;
return function (...args) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() = {
}, delay);
};
}

const handleClick = function () {
};

const debouncedClick = debounce(handleClick, 300);

debouncedClick(); debouncedClick();

setTimeout(() = {
debouncedClick();
}, 400);

Options:
a) "Button clicked!" three times
b) "Button clicked!" once
c) "Button clicked!" twice
d) No output

Answer:
b) "Button clicked!" once

Explanation:
The debounce function ensures that "handleClick" is invoked only after 300ms have passed since the last invocation. The first two calls to "debouncedClick" happen within 300ms of each other, so "handleClick" is not executed for either of them. The third call happens after 400ms, which is outside the 300ms delay window, so "handleClick" is executed and "Button clicked!" is logged to the console.

#debounce #functionthrottling #interview #questionsandanswers #coding #javascriptinterviewquestions
Рекомендации по теме
welcome to shbcf.ru