|
| 1 | +const input = document.querySelector("input"); |
| 2 | +const debouncedValue = document.getElementById("debouncedValue"); |
| 3 | +const inputValue = document.getElementById("value"); |
| 4 | + |
| 5 | +input.addEventListener("input", updateValue); |
| 6 | + |
| 7 | +const handler = updateValueDebounced(updateValue1, 2000); |
| 8 | +input.addEventListener("input", handler); |
| 9 | + |
| 10 | +function updateValue(e) { |
| 11 | + console.log("updating value"); |
| 12 | + inputValue.textContent = e.target.value; |
| 13 | +} |
| 14 | + |
| 15 | +function updateValue1(e) { |
| 16 | + debouncedValue.textContent = e.target.value; |
| 17 | +} |
| 18 | + |
| 19 | +function updateValueDebounced(fn, delay) { |
| 20 | + let timeoutId; |
| 21 | + console.log(" debounced call"); |
| 22 | + return function (...args) { |
| 23 | + clearTimeout(timeoutId); |
| 24 | + timeoutId = setTimeout(() => fn(...args), delay); |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +// debounce seacrh Practice |
| 29 | + |
| 30 | +const searchInput = document.getElementById("searchInput"); |
| 31 | +const results = document.getElementById("results"); |
| 32 | + |
| 33 | +searchInput.addEventListener("input", debounce(handleSearch, 1000)); |
| 34 | + |
| 35 | +function handleSearch(event) { |
| 36 | + const query = event.target.value; |
| 37 | + // Simulate an API call with a timeout |
| 38 | + console.log("Searching for:", query); |
| 39 | + setTimeout(() => { |
| 40 | + const fakeResults = [ |
| 41 | + "apple", |
| 42 | + "banana", |
| 43 | + "orange", |
| 44 | + "grape", |
| 45 | + "watermelon", |
| 46 | + "kiwi", |
| 47 | + "mango", |
| 48 | + "mango smoothie", |
| 49 | + "strawberry", |
| 50 | + ].filter((item) => item.includes(query.toLowerCase())); |
| 51 | + displayResults(fakeResults); |
| 52 | + }, 300); |
| 53 | +} |
| 54 | + |
| 55 | +function displayResults(items) { |
| 56 | + results.innerHTML = ""; |
| 57 | + items.forEach((item) => { |
| 58 | + const li = document.createElement("li"); |
| 59 | + li.textContent = item; |
| 60 | + results.appendChild(li); |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +function debounce(fn, delay) { |
| 65 | + let timeoutId; |
| 66 | + console.log("+++++++++debounce call+++++++++"); |
| 67 | + return function (...args) { |
| 68 | + clearTimeout(timeoutId); |
| 69 | + timeoutId = setTimeout(() => fn(...args), delay); |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +// Debounce keypress and keyup events |
| 74 | +const inputField = document.querySelector("input"); |
| 75 | + |
| 76 | +const debouncedKeyPressHandler = debounce(handleKeyPress, 500); |
| 77 | +const debouncedKeyUpHandler = debounce(handleKeyUp, 500); |
| 78 | + |
| 79 | +inputField.addEventListener("keypress", debouncedKeyPressHandler); |
| 80 | +inputField.addEventListener("keyup", debouncedKeyUpHandler); |
| 81 | + |
| 82 | +function handleKeyPress(event) { |
| 83 | + console.log("Key Pressed:", event.key); |
| 84 | + // You can add your logic here for keypress event |
| 85 | +} |
| 86 | + |
| 87 | +function handleKeyUp(event) { |
| 88 | + console.log("Key Released:", event.key); |
| 89 | + // You can add your logic here for keyup event |
| 90 | +} |
| 91 | + |
| 92 | +const throttle = (func, limit) => { |
| 93 | + let inThrottle; |
| 94 | + return function (...args) { |
| 95 | + if (!inThrottle) { |
| 96 | + func.apply(this, args); |
| 97 | + inThrottle = true; |
| 98 | + setTimeout(() => (inThrottle = false), limit); |
| 99 | + } |
| 100 | + }; |
| 101 | +}; |
| 102 | + |
| 103 | + |
| 104 | +const container = document.getElementById("throttle-container"); |
| 105 | +// const handleScroll = function () { |
| 106 | +// console.log("scrolling..."); |
| 107 | +// }; |
| 108 | + |
| 109 | +const handleScroll = throttle(() => { |
| 110 | + const scrollTop = window.scrollY; |
| 111 | + console.log("Scroll position:", scrollTop); |
| 112 | + |
| 113 | + // Expensive calculations |
| 114 | + if (scrollTop > 500) { |
| 115 | + console.log("User scrolled past 500px"); |
| 116 | + } |
| 117 | +}, 500); |
| 118 | + |
| 119 | +container.addEventListener("scroll", handleScroll); |
0 commit comments