If you call a function repeatedly, how do you make it execute in order?

<script>
  test()
  test()
  test()
  function test(){
    setTimeout(function () {
      console.log("test")
    },2000)
  }``
</script>

print at the same time after 2000 milliseconds, how can I print every 2000 milliseconds

Oct.23,2021

(function($loop, $timeout){
    function test(){
       console.log('test ['+$loop+']');
       
       if(--$loop > 0)
           setTimeout(test, $timeout);
    }
    
    setTimeout(test, $timeout)
})(3, 2000);
Menu