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 questionsThe 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.
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.
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()).
Keep practicing
Take the free CRT-600 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other CRT-600 practice questions
- Question 2A developer has the function, shown below, that is called when a page loads. function onl…
- Question 3A developer needs to test this function: 01 const sum3 = (arr) => ( 02 if (!arr.length) r…
- Question 4Refer to the code below: Const resolveAfterMilliseconds = (ms) => Promise.resolve ( setTi…
- Question 10Refer to the following code block: class Animal{ constructor(name){ this.name = name; } m…
- Question 11A developer needs to debug a Node.js web server because a runtime error keeps occurring a…
- Question 12Refer to the string below: const str = 'Salesforce'; Which two statements result in the w…
