Skip to content

Commit 21ce68d

Browse files
committed
debouncing, string methods
1 parent ec6701e commit 21ce68d

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

Javascript_Study_Month/week2/stringMethods4.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ console.log(`Substr of string ${s8} from 0 to 15 is `+s8.substr(5, 15)); // subs
1515
// // The substring() method does not change the original string.
1616
let s9 = "substring() method returns the part of the string between the two specified indexes";
1717
console.log(`Substring of string ${s9} from 0 to 15 is `+s9.substring(5, 15)); // substring() method
18-
18+
5
1919
// 21. toLowerCase
2020
// // The toLowerCase() method returns the calling string value converted to lower case.
2121
// // The toLowerCase() method does not change the original string.

debouncing.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// A simple debouncing example in JavaScript
2+
// Debouncing ensures that a function is not called too frequently.
3+
4+
function debounce(func, wait) {
5+
let timeout;
6+
return function(...args) {
7+
const later = () => {
8+
clearTimeout(timeout);
9+
func(...args);
10+
};
11+
clearTimeout(timeout);
12+
timeout = setTimeout(later, wait);
13+
};
14+
}
15+
// Example usage:
16+
const logMessage = () => {
17+
console.log('Function executed!');
18+
};
19+
const debouncedLogMessage = debounce(logMessage, 2000);
20+
21+
// Simulate rapid calls to the debounced function
22+
debouncedLogMessage();
23+
debouncedLogMessage();
24+
debouncedLogMessage();
25+
// Only the last call will execute after 2 seconds of inactivity

0 commit comments

Comments
 (0)