I want to Implement a function which can run a given function after a delay.
Arguments:
And this was my code
let cb = function(x) {
console.log(x);
};
const doShortly = function(callback, delay, data) {
let result = setTimeout(callback(data), delay);
return result;
};
console.log(doShortly(cb, 500, 'hi'));
I get TypeError [ERR_INVALID_CALLBACK] when I run the code. May I know how to fix this. TIA.
You can use setTimeout or setInterval for that directly. All the
params after the delay param, will be passed to the callback function in the same order :)
let cb = function(x) {
console.log(x);
};
const doShortly = function(callback, delay, data) {
let result = setTimeout(callback, delay, data);
return result;
};
console.log(doShortly(cb, 500, 'hi')); // or: setTimeout(cb, 500, 'hi')