0

is there any native javascript function to run script one by one (no jquery). I describe what I mean.

My html file is:

<html><body>
  Hello
  <script type="text/javascript" src="sleep.js"></script>
  <script type="text/javascript">
  alert('boo');
  </script>
</body></html>

sleep.js:

setTimeout(myFunction, 3000);
function myFunction(){
    alert('hi');
}

When I load my page alert 'boo' shows immediatelly and after 3 seconds it shows alert 'hi'. But I want to execute scripts one by one (as they follow in html file), so first script sleep.js should be executed as first one, it sleeps for 3 seconds, then shows 'hi', and then second alert shows 'boo'.

How this can be done?

P.S.This is just a short example, I need if for any scripts, for example if first script will be google analytics code, it should be executed first, then if first script finished run second script, then third script, one by one.

peter
  • 4,289
  • 12
  • 44
  • 67

0 Answers0