Skip to content

Commit c2a2fd4

Browse files
committed
Optimize timeout
1 parent 07b10d9 commit c2a2fd4

1 file changed

Lines changed: 5 additions & 6 deletions

File tree

JavaScript/5-timeout.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44

55
const timeout = (msec, f) => {
66
let timer = setTimeout(() => {
7-
if (timer) console.log('Function timedout');
7+
if (timer) console.log('Function timed out');
88
timer = null;
99
}, msec);
1010
return (...args) => {
11-
if (timer) {
12-
clearTimeout(timer);
13-
timer = null;
14-
return f(...args);
15-
}
11+
if (!timer) return;
12+
clearTimeout(timer);
13+
timer = null;
14+
return f(...args);
1615
};
1716
};
1817

0 commit comments

Comments
 (0)