Ray Rahke
03/15/2024, 4:17 PMwindow.onload()
window.onload {
val canvas = document.getElementById("canvas")
}
getting error
Task :compileKotlinJs FAILED
e: Reference has a nullable type '((Event) -> dynamic)?', use explicit '?.invoke()' to make a function-like call instead
e: Type mismatch: inferred type is () -> Unit but Event was expected
What is the syntax for this?
I tried window?.invoke().onload {
and that also got an errorRuckus
03/15/2024, 4:24 PMwindow.onload?.invoke { ... }
It's onload
that's nullable, not window
.
https://kotlinlang.org/api/latest/jvm/stdlib/org.w3c.dom/-global-event-handlers/Ray Rahke
03/15/2024, 4:43 PMDarryl Miles
03/15/2024, 7:20 PMfun windowOnLoadOrTimeout(function: ((Event?) -> Unit)) {
if(document.body != null) { // late we missed "load" event
window.setTimeout({
function.invoke(null)
null // lambda return is fussy here
}, 0)
} else {
window.addEventListener("load") { event -> // early need to wait for "load" event
function.invoke(event)
}
}
}
windowOnLoadOrTimeout() {
// my work here
}
The purpose of this, is that it will always run, even when it missed the window "load" eventRay Rahke
03/16/2024, 9:01 AMType mismatch: inferred type is () -> Unit but Event was expected
Ray Rahke
03/16/2024, 9:01 AMwindow.onload.invoke() {
...
}
Darryl Miles
03/16/2024, 5:56 PMsetTimeout()
does not work https://pl.kotl.in/Dx8U87Xhk then my original code more or less runsDarryl Miles
03/16/2024, 5:56 PMwindow.onload.invoke() {}