-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathrepeatAction.js
More file actions
26 lines (21 loc) · 787 Bytes
/
repeatAction.js
File metadata and controls
26 lines (21 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* @author 5antos#4876
* @param {number} amount Amount of times to run the callback
* @param {function} callback Function to run every loop
* @param {function} finishCallback Function to run after running all loops
* @param {number} time Interval time, in milliseconds (defaults to 0, running every millisecond)
* @returns {unknown}
*/
function repeatAction(amount, callback, finishCallback, time=0) {
if (amount--) callback()
if (amount > 0) return setTimeout(() => repeatAction(amount, callback, finishCallback, time), time)
// Section for code to be executed when the action has repeated the provided amount of times
finishCallback()
}
// Usage Examples:
repeatAction(
5,
() => console.log('Digging...'),
() => console.log('You found a bone!')
3000
)