# Running Your Code at the Right Time

**URL:** https://forum.kirupa.com/t/running-your-code-at-the-right-time/644971
**Category:** programming
**Created:** [November 17, 2020, 8:58pm UTC](https://forum.kirupa.com/t/running-your-code-at-the-right-time/644971 "2020-11-17T20:58:24Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![vmars316](https://avatars.discourse-cdn.com/v4/letter/v/bcef8e/32.png) [@vmars316](https://forum.kirupa.com/u/vmars316)
#### Post date: [November 17, 2020, 8:58pm UTC](https://forum.kirupa.com/t/running-your-code-at-the-right-time/644971/1 "2020-11-17T20:58:25Z")

</div>

I have read your " Running Your Code at the Right Time"  
so I wrote some code to test it out (chrome):  
I put the scripts at bottom of page , and expected them to run after the page was fully loaded . But the [h5] didn’t display until after both scripts were run .  
? Why doesn’t the [h5] display before the scripts were run ?  
Also , in general:  
? Is script positioned ‘at page bottom’ the best place to ‘add.listeners’ ?  
I haven’t been here for a while , and have forgotten how to display code , Each Forum has their own protocol .  
I wish you would have a sticky post , as Top Post titled “How to display code” in your Posts" . I’ll try a few .

```auto
<!DOCTYPE html>
<html>
  <head>
       <style>
	   
	   </style>
	   <script>
function pageHasFullyLoaded() {
  alert("1) Page has FULLY loaded");
}

function firstScriptHasCompleted() {
  alert("2) first Script Has Completed ! ");
}
	   </script>
  </head>
  <body>
  
<h5>Hello World!</h5>

<script>
pageHasFullyLoaded()
</script>

<script>
    firstScriptHasCompleted()
</script>

</body>
</html>

```

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [November 17, 2020, 11:33pm UTC](https://forum.kirupa.com/t/running-your-code-at-the-right-time/644971/2 "2020-11-17T23:33:27Z")

</div>

Hey @vmars316! Using `alert` is often a bit strange in how it interrupts everything else on the page, but if you set a breakpoint and step through the code in the debugger, the order of code running is maintained:

 ![image](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/2X/0/08228cfaace70800aa0e974815227ea84329e89d.jpeg)

You can verify by printing the contents of our `h5` element:

```
<!DOCTYPE html>
<html>

<head>
  <style>

  </style>
  <script>
    function pageHasFullyLoaded() {
      alert(document.querySelector("h5").textContent);
    }

    function firstScriptHasCompleted() {
      console.log("2) first Script Has Completed ! ");
    }
  </script>
</head>

<body>

  <h5>Hello World!</h5>

  <script>
    pageHasFullyLoaded()
  </script>

  <script>
    firstScriptHasCompleted()
  </script>

</body>

</html>

```

If the code ran earlier than when it should, the value of the h5 tag won’t be displayed in the alert 🙂

As for the best place to add listeners, I prefer it at the bottom of the page. The only time might be if I need to intercept some event immediately prior to the page having fully loaded.

For the code commenting question, yes - this comes up enough times where we may need a dedicated sticky topic haha. Here is a collection of the various ways we have on these forums for formatting code: [Lists in html - italics - #9 by kirupa](http://forum.kirupa.com/t/lists-in-html-italics/644817/9)

🐼

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [November 18, 2020, 11:43am UTC](https://forum.kirupa.com/t/running-your-code-at-the-right-time/644971/3 "2020-11-18T11:43:23Z")

</div>

To add to what kirupa said, though the browser may have read and interpreted the HTML tags before running the code, it doesn’t mean it has _displayed_ the HTML to the screen. This process can be slightly deferred, and generally not something that’s a problem normally because it usually happens so fast. But because alerts (and prompts and confirm dialogs) are a kind of UI that also blocks the main UI thread, you can get into situations where you’re in a time period between reading HTML tags and displaying them, where, if a dialog like alert appears, you are able to see that the HTML has not yet rendered, and until the dialog is dismissed, block it from being rendered.

BTW, there is a little trick if you want to run something like an alert after the page is rendered. You can go through a double-requestAnimationFrame callback. requestAnimationFrame allows you to run code right before you render, and if you do two, it will run code after the first render (and before the second render).

```
requestAnimationFrame(() => requestAnimationFrame(pageHasFullyLoaded))
```
