๐พdelay(f, s)
delay the execution of a function/method by s seconds.
JS โฉ technique โฉ decorator โฉ ๐พ delay(f, s)
replit๏ผdelay(f, s)
// ๐ delay.js
const { log } = console;
// โญ๏ธ decorator: "delay"
// (delay execution by s seconds)
function delay(f, s) {
// wrapper
return function wrapper(...args) {
// delay execution
setTimeout(
// โ msut use "arrow function" here.
// โ `this` context is taken from the `wrapper`.
() => {
// add single-quotes ('') to string arguments
let arglist = args
.map(arg => typeof arg === 'string' ? `'${arg}'` : arg)
.join(' ,');
// log execution time, function name, arguments
log(`${s}: ${f.name}(${arglist}) ...`);
// โญ๏ธ forward function call to `f`
f.apply(this, args); // โ `this` context taken from `wrapper`
},
// execute after s seconds
s * 1000
);
}
}
// export
module.exports = { delay };
Last updated
Was this helpful?