JSFiddle - React, Tailwind, and code Playground

HTML

<button id="loop_controler">pause</button>

JavaScript

var array = [1,2,3,4,5,6,7];
    var pause = false;
    var timer;
    
    function execute(array,i){
        var pauseTime = 0;
        if (pause){
            pauseTime = 2147483647;  //max value for timeout     
        }
        timer = setTimeout(function(){
               setTimeout(function () {
                   alert(array[i]);
                   if (array.length > i)
                       execute(array,i+1);
               }, 1000);
        }, pauseTime);
        console.log('set ' + timer);
    }
    
    $('document').ready(function(){
    
        $('#loop_controler').click(function(){
            if ($(this).text() == 'pause'){
              pause = true;
              $(this).text('resume');
            } else {
              clearTimeout(timer);
              console.log('clear ' + timer);
              pause = false;
              $(this).text('pause');
            }
        });
        
        execute(array,0);
    });