Reputation: 3766
I have that function in the class:
private function fireItemCreated(data: ByteArray): void {
setTimeout(function(): void {
var event: ItemCreatedEvent = new ItemCreatedEvent(data);
dispatchEvent(event);
}, 1000);
}
This function called to dispatch item created event when image thumbnail created.
But it delays event on some time to prevent user interface freezes. And I'm guessing what could be happen if garbage collector executes after fireItemCreated function call but before timer event. Does the closure will be removed or it stays until it will be executed?
Upvotes: 2
Views: 160
Reputation:
It can't happen.
If the function is called then setTimeout
is called. The function-object passed to setTimeout
creates a strong closure-binding with the linked execution context and all setTimeout
callback functions are protected (strongly held) by the host engine (imagine there is an invisible var timeouts = []
you can't access). It wouldn't be fun if timers were magically swallowed up by the evil Grime Captain.
Good question and Happy coding.
The issue described can actually happen in some other languages and their implementations of Timers. See .NET's Threading.Timer Class and the notes.
Upvotes: 4