I'm currently working on a JavaScript exercise in my code. I encountered an issue where I am trying to assign a reference to the setTimeout function to ogTimeout on the second line since I plan to redefine setTimeout later.
However, when I run the code, I receive the error "ReferenceError: Cannot access 'setTimeout' before initialization". While I understand that this error typically indicates that setTimeout needs to be initialized, it is already an existing function in this case, leaving me confused about what could be causing the problem.
Any assistance would be greatly appreciated. Thank you!
const timeouts = [];
const ogTimeout = setTimeout;
let setTimeout = function(method, timeInMs) {
const timeoutRef = ogTimeout(method, timeInMs);
timeouts.push(timeoutRef);
}
const clearAllTimeouts = function() {
while (timeouts.length) {
const timeoutRef = timeouts.pop();
clearTimeout(timeoutRef);
}
}
setTimeout(() => console.log("Callback after 5 seconds."), 5000);
I've attempted to research the ReferenceError and its related details without success.