N
N
Null_lol2019-05-22 18:09:01
JavaScript
Null_lol, 2019-05-22 18:09:01

Why is the code execution order different in Chrome and Firefox?

Code like this:

<script>
  setTimeout(function() {
    alert('setTimeout-1')
  }, 0);
  setTimeout(function() {
    alert('setTimeout-2')
  }, 0);
  setTimeout(function() {
    alert('setTimeout-3')
  }, 0);
</script>
<p>paragraph-1</p>
<script>
  alert('one');
</script>
<p>paragraph-2</p>
<script>
  alert('two');
</script>
<p>paragraph-3</p>
<script>
  alert('three');
</script>
<p>paragraph-4</p>
<script>
  alert('four');
</script>

In Firefox, alert (one, two, three, fout) is executed first, and only then the setTimeout callback function is called. But in Chrome, first alert(one and two), then callback functions, and only then everything else. Why is this happening?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question