# Execute some JavaScript after initial render

**URL:** <https://discourse.elm-lang.org/t/execute-some-javascript-after-initial-render/517>\
**Category:** Learn\
**Created:** [January 11, 2018, 4:35am UTC](https://discourse.elm-lang.org/t/execute-some-javascript-after-initial-render/517 "2018-01-11T04:35:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bandicoot](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/bandicoot/32/429_2.png) [@Bandicoot](https://discourse.elm-lang.org/u/Bandicoot)\
**Post date:** [January 11, 2018, 4:35am UTC](https://discourse.elm-lang.org/t/execute-some-javascript-after-initial-render/517/1 "2018-01-11T04:35:48Z")

</div>

I have a JavaScript file that embeds a Elm application, and I would like run some JavaScript code after the first rendering of the Elm application. Here’s what I have so far:

```
var app = Elm.Page.Gadget.embed(content, flags);
window.setTimeout(function() {
    ...
});

```

Is that the way to do it? From what I’m seeing, it appears that the code inside the `setTimeout` function is not executed strictly after the Elm app has rendered itself.

---

<div class="post-metadata">

**Author:** ![joakin](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/joakin/32/171_2.png) [@joakin](https://discourse.elm-lang.org/u/joakin)\
**Post date:** [January 11, 2018, 9:07am UTC](https://discourse.elm-lang.org/t/execute-some-javascript-after-initial-render/517/2 "2018-01-11T09:07:06Z")

</div>

I’ve done it before by notifying a port from init and then on JS queueing with requestAnimationFrame , which has seemed to work fine.

- [https://github.com/joakin/pinboard-unread/blob/43e2eebbf62996f90f7ed2795b0852ecdd68092c/src/index.js#L13](https://github.com/joakin/pinboard-unread/blob/43e2eebbf62996f90f7ed2795b0852ecdd68092c/src/index.js#L13)
- [https://github.com/joakin/pinboard-unread/blob/43e2eebbf62996f90f7ed2795b0852ecdd68092c/src/Main.elm#L51](https://github.com/joakin/pinboard-unread/blob/43e2eebbf62996f90f7ed2795b0852ecdd68092c/src/Main.elm#L51)

I’m not sure if that’s the recommended or best way though, I’d love to hear from others!

---

<div class="post-metadata">

**Author:** ![edkv](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@edkv](https://discourse.elm-lang.org/u/edkv)\
**Post date:** [January 11, 2018, 9:50am UTC](https://discourse.elm-lang.org/t/execute-some-javascript-after-initial-render/517/3 "2018-01-11T09:50:57Z")

</div>

Check out the [`elm-lang/animation-frame`](http://package.elm-lang.org/packages/elm-lang/animation-frame/1.0.1/AnimationFrame) package. You can use the `times` function to setup a subscription on the initial render (based on a boolean flag in your model, something like `isFirstRender`) and you will get a message after the view is rendered. Then use a port to run your Javascript code.

---

<div class="post-metadata">

**Author:** ![MarkHamburg](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@MarkHamburg](https://discourse.elm-lang.org/u/MarkHamburg)\
**Post date:** [January 11, 2018, 4:11pm UTC](https://discourse.elm-lang.org/t/execute-some-javascript-after-initial-render/517/4 "2018-01-11T16:11:58Z")

</div>

Another alternative that doesn’t depend on the sequencing guarantees made or more often not made by Elm’s libraries — for example, animation-frame says that it generates messages in lockstep with the browser rendering but does not say whether your app will get to process the message before or after the corresponding render — is to do all of this in JavaScript. There are a variety of ways from JavaScript, some more skanky but possibly more performant than others, of watching for when DOM elements are created and triggering code. So, if you can set up something that watches for when your root element is created, you have your hook. Whether you want to deal with Elm sequencing ambiguity or what can feel like hacks in JavaScript is a call you need to make.

Mark

P.S. This is what I use. It feels super skanky since it triggers off of CSS animations but it works. It would be nice to know that such tricks aren’t needed in 2018 (this was written in 2012), but I haven’t see a reference to a definitive, performant method of catching DOM node creation in modern browsers. [https://davidwalsh.name/detect-node-insertion](https://davidwalsh.name/detect-node-insertion)

---

<div class="post-metadata">

**Author:** ![edkv](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@edkv](https://discourse.elm-lang.org/u/edkv)\
**Post date:** [January 11, 2018, 5:36pm UTC](https://discourse.elm-lang.org/t/execute-some-javascript-after-initial-render/517/5 "2018-01-11T17:36:43Z")

</div>

> [@MarkHamburg](#):
>
> It would be nice to know that such tricks aren’t needed in 2018 (this was written in 2012), but I haven’t see a reference to a definitive, performant method of catching DOM node creation in modern browsers.

Seems like the best way to do it is [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) which is [supported in all modern browsers](https://caniuse.com/#feat=mutationobserver). I haven’t used it myself though.
