# 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:** 1\
**Showing post:** 2

<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)

🐼

---

_[View the full topic](https://forum.kirupa.com/t/running-your-code-at-the-right-time/644971)._
