Skip to content

Commit 332fb17

Browse files
authored
Update debounce_throttle.md
1 parent e3b1a2b commit 332fb17

File tree

1 file changed

+14
-36
lines changed

1 file changed

+14
-36
lines changed

debounce_throttle.md

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,23 @@
11
### Debounce
2-
- wait 0.5 seconds and reset to 0.5 every new bounce
2+
- hold 0.5 second
3+
- reset to 0.5 on every new call
4+
- run on 0.5 reached
5+
- non-blocking
36
```sh
4-
# non-blocking
5-
echo 5 > flagfile # 5 x 0.1 = 0.5 s
6-
(
7-
for (( i=0; i < 5; i++ )); do
8-
sleep 0.1
9-
s=$(( $( cat flagfile ) - 1 ))
10-
if (( $s > 0 )); then
11-
echo $s > flagfile
12-
else
13-
rm -f flagfile
14-
COMMAND1
15-
fi
16-
done
17-
) &> /dev/null &
18-
```
19-
- allow only 1st run within 1 seconds (run > wait)
20-
21-
```sh
22-
# non-blocking
23-
if [[ ! -e flagfile ]]; then
24-
touch flagfile
25-
(
7+
echo 5 > flagfile # newcall
8+
( for (( i=0; i < 5; i++ )); do
9+
sleep 0.1 # duration between each call
10+
s=$(( $( cat flagfile ) - 1 ))
11+
(( $s == 4 )) && i=0 # new call - reset count
12+
if (( $s > 0 )); then
13+
echo $s flagfile
14+
else
2615
COMMAND1
2716
COMMAND2
28-
sleep 1 #####
29-
3017
rm -f flagfile
31-
) &
32-
fi
18+
fi
19+
done ) &> /dev/null &
3320

34-
# within while loop
35-
if [[ ! -e flagfile ]]; then
36-
touch flagfile
37-
COMMAND1
38-
COMMAND2
39-
sleep 1 #####
40-
41-
rm -f flagfile
42-
fi
4321
```
4422

4523
### Throttle

0 commit comments

Comments
 (0)