CRT-600 practice question 22 of 30

Refer to the code below: let timeFunction =() => { console.log(‘Timer called.”); }; let timerId = setTimeout (timedFunction, 1000); Which statement allows a developer to cancel the scheduled timed function?

Full set: 223 questions
Answer options
A: removeTimeout(timedFunction);
B: removeTimeout(timerId);
C: clearTimeout(timerId); Correct answer
D: clearTimeout(timedFunction);
Correct Answer:
C. clearTimeout(timerId);
Explanation

The setTimeout() function schedules a function to be executed after a specified delay and returns a numeric timeoutID. This ID acts as a unique identifier for the scheduled timer. To cancel this pending execution, the clearTimeout() method must be called, passing the timeoutID as its sole argument. In the provided code, timerId holds this identifier, making clearTimeout(timerId) the correct statement to cancel the scheduled function.

Why Incorrect

A. removeTimeout(timedFunction);: removeTimeout is not a standard JavaScript function, and the timer ID is required for cancellation, not the function reference.
B. removeTimeout(timerId);: The standard function for canceling a timeout is clearTimeout(), not removeTimeout.
D. clearTimeout(timedFunction);: The clearTimeout() function expects the numerical ID returned by setTimeout(), not the callback function itself.

References

1. MDN Web Docs, clearTimeout(): "The clearTimeout() static method cancels a timeout previously established by calling setTimeout(). The timeoutID is the ID of the timeout you want to cancel. This ID is returned by the corresponding call to setTimeout()." (Source: Mozilla Developer Network, WindowOrWorkerGlobalScope.clearTimeout() page).

2. MDN Web Docs, setTimeout(): "The returned timeoutID is a positive integer value which identifies the timer created by the call to setTimeout(). This value can be passed to clearTimeout() to cancel the timeout." (Source: Mozilla Developer Network, WindowOrWorkerGlobalScope.setTimeout() page).

3. WHATWG HTML Living Standard: "The clearTimeout(id) method must clear the entry identified by id from the list of active timers of the WindowOrWorkerGlobalScope object on which the method was invoked, if any such entry exists." (Source: WHATWG, HTML Living Standard, Section 8.7.2: Canceling timers, clearTimeout()).

Scroll to Top