Последовательное выполнение кода

Подскажите пожалуйста как последовательно выполнить эти 4 функции?

setTimeout(function() {
        $('#div4').replaceWith("_");
        $('#div3').replaceWith("4");
        $('#div2').replaceWith("3");
        $('#div1').replaceWith("2");
    }, 5000);
    
setTimeout(function() {
        $('#div4').replaceWith("_");
        $('#div3').replaceWith("_");
        $('#div2').replaceWith("4");
        $('#div1').replaceWith("3");
}, 5000);

setTimeout(function() {
        $('#div4').replaceWith("_");
        $('#div3').replaceWith("_");
        $('#div2').replaceWith("_");
        $('#div1').replaceWith("4");
}, 5000);

setTimeout(function() {
        $('#div4').replaceWith("_");
        $('#div3').replaceWith("_");
        $('#div2').replaceWith("_");
        $('#div1').replaceWith("_");
}, 5000);
Заранее спасибо!


Ответы (1 шт):

Автор решения: Михаил Камахин

Можно так

let time = 0;

setTimeout(function() {
  $('#div4').text("_");
  $('#div3').text("4");
  $('#div2').text("3");
  $('#div1').text("2");
}, time += 5000);

setTimeout(function() {
  $('#div4').text("_");
  $('#div3').text("_");
  $('#div2').text("4");
  $('#div1').text("3");
}, time += 5000);

setTimeout(function() {
  $('#div4').text("_");
  $('#div3').text("_");
  $('#div2').text("_");
  $('#div1').text("4");
}, time += 5000);

setTimeout(function() {
  $('#div4').text("_");
  $('#div3').text("_");
  $('#div2').text("_");
  $('#div1').text("_");
}, time += 5000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="div4">4</div>
<div id="div3">3</div>
<div id="div2">2</div>
<div id="div1">1</div>

→ Ссылка