How to use SetTimeout in Rescript and Document.getElementById

React.useEffect1(()=>{
let timeout = Js.Global.setTimeout(()=>{
let vaall = ReactDOM.querySelector(“#txt”)
Js.log2(“vall”,vaall)
let _= vall->Js.String2.split(%re(“/[\n,]/”))
setData([…data, …vaall.split(/[\n,]/)]);

},1000)

if nwcount == 0 {
Js.Global.clearTimeout(timeout)
}
None

},[nwcount])

Hi All,
Getting same issue. Could someone help in knowing how to use document.getElementById for DOM manipulation to get the element from HTML code using Id.

Thanks

For a lot of the wider core js functions, you’ll need to write your own bindings for it. For getElementById it would look something like this:

@scope("document") external getElementById: string => option<Js.Dom.element> = "getElementById"

You can read on some of the reasoning on this post, and the one it links to:

1 Like

It looks like @spyder has taken control of rescript-webapi? (congrats)
Its not super approachable, but its not much less approachable than the underlying API if you know what you’re trying to do.

https://thespyder.github.io/rescript-webapi/api/Webapi/Dom/Document/

1 Like

Yes, we haven’t announced it yet but the big project I was working on was cancelled so I convinced the company to hand the repository to me. I and one of the other developers were already the primary contributors after we forked it from bs-webapi.

The lack of docs and “getting started” guidance is something it could use help with for sure. It can offer a lot of benefit, and save a lot of time from people writing their own bindings, if used correctly. The examples are actually a much better place to start than the docs right now:

With the right set of open statements at the top, it becomes quite natural to write document->getElementById("foo").

3 Likes