Skip to content

Conversation

@hamishwillee
Copy link
Collaborator

@hamishwillee hamishwillee commented Nov 24, 2025

This updates Window.setTimeout() and WorkerGlobalScope.setTimeout() for trusted types, following the same approach as #42042

  • More or less full docs in the windows method. There is a page header warning in the top and a security considerations section in the body.
  • worker method has same top section, but description and examples are cross linked to window method docs. The header links to the security considerations in the window method topic.
  • Examples do not cover the use of TT. We have pretty strong warnings up the top about using this feature at all, so I don't see we need to show TT use. I have linked to the eval() example on this added in TrustedTypes: Function() constructor + eval() #42462

This also did some consistency fixes across both this and the setInterval methods so that they have the same name for the function param (func) and the arguments are referred to as param rather than args - which is consistent with the template for web apis.

There were a few layout change and improvements to the docs - these map the similar changes done previously for setInterval()

This is part of project being tracked in #41507

@hamishwillee hamishwillee requested a review from a team as a code owner November 24, 2025 04:58
@hamishwillee hamishwillee requested review from sideshowbarker and removed request for a team November 24, 2025 04:58
@github-actions github-actions bot added Content:WebAPI Web API docs size/m [PR only] 51-500 LoC changed labels Nov 24, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Nov 24, 2025

@sideshowbarker sideshowbarker removed their request for review November 25, 2025 09:07
@hamishwillee hamishwillee requested a review from a team as a code owner December 2, 2025 05:51
@hamishwillee hamishwillee requested review from pepelsbey and removed request for a team December 2, 2025 05:51
@github-actions github-actions bot added the Content:Firefox Content in the Mozilla/Firefox subtree label Dec 2, 2025
@hamishwillee
Copy link
Collaborator Author

@pepelsbey Can you please review?

@hamishwillee
Copy link
Collaborator Author

@wbamberg Would you also/instead consider reviewing this. I ask because it is for TrustedTypes, and we've both been looking at all of those.

@wbamberg
Copy link
Collaborator

wbamberg commented Dec 5, 2025

I'm happy to look at this, yes, but it will take me a few days until I can find the time.

Comment on lines 104 to 109
Notice that the first function does not create a 5-second "pause" before calling the second function.
Instead, the first function is called, but waits 5 seconds to execute.
While the first function is waiting to execute, the second function is called, and a 3-second wait is applied to the second function before it executes.
Since neither the first nor the second function's timers have completed, the third function is called and completes its execution first.
Then the second follows.
Then finally the first function is executed after its timer finally completes.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it me or is this explanation very confusing? It doesn't seem to distinguish the setTimeout function and the callback argument, when it talks about "functions". Like:

Instead, the first function is called, but waits 5 seconds to execute.

Surely the first function (i.e. the first setTimeout` call) is called, and executes, and returns. It's not waiting to execute (itself). It's scheduled the callback to run after 5 seconds.

While the first function is waiting to execute, the second function is called, and a 3-second wait is applied to the second function before it executes.

What is "it" here? The second function? Like, the second setTimeout? But that has executed, it's the callback that hasn't.

And:

Since neither the first nor the second function's timers have completed, the third function is called and completes its execution first.

...implies that the third function is only called if the first two haven't completed ("since...the third function is called"), but that's completely unrelated.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I didn't think it worth the effort to tidy it. Arguably in reference, and at this point in JavaScript you shouldn't even need such an example and comment on what asynchronous means.

Fixed in 5008d20

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Hamish. It is true that these are unrelated to your PR, and I would not have commented if it had not been in the diffs. But if it's in the diffs, I have to read it, and if I read it I have to comment :). And I appreciate your willingness to leave things better than you found them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're willing to add a comment with a fix suggestion I will always try to integrate it. I'm just explaining why I didn't in the first place. Excuses, excuses!

myArray.myMethod(); // prints "zero,one,two"
myArray.myMethod(1); // prints "one"
setTimeout(myArray.myMethod, 1000); // Alerts "[object Window]" after 1 second
setTimeout(myArray.myMethod, 1500, "1"); // Alerts "undefined" after 1.5 seconds
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So maybe I've just eaten too much tofurkey, but this seems like a very complex example to illustrate a relatively simple thing. Why can't we demonstrate using something like:

const myObject = {
	log() {
    	console.log(`myProperty: ${this.myProperty}`)
	},
	myProperty: 12,
}

myObject.log();
// myProperty: 12

setTimeout(myObject.log, 1000);
// myProperty: undefined
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is. Again, did not really even look at this as orthogonal to "the point".

Anyhow, I have mostly fixed in 40a026c

I say mostly, because I used this approach to highlight the problem, and the arrow solution. However I don't understand how to integrate it with bind() (and I don't want to at this point). So the example is still complicated in the final part.

Co-authored-by: wbamberg <will@bootbonnet.ca>
Copy link
Collaborator

@wbamberg wbamberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 thank you!

@wbamberg wbamberg merged commit 21ed9a1 into mdn:main Dec 30, 2025
7 checks passed
@hamishwillee hamishwillee deleted the tt_setTimeout_n_fixes branch December 30, 2025 03:27
@hamishwillee
Copy link
Collaborator Author

Thank you @wbamberg - wish you the very happiest new year.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:Firefox Content in the Mozilla/Firefox subtree Content:WebAPI Web API docs size/m [PR only] 51-500 LoC changed

3 participants