JavaScript: setTimeout yielding check

by Mauno Vähä

HTML

<img src="http://www.mibugs.com/wp-content/uploads/2014/05/spinner.gif" widht="100" height="100" class="spinner" />
<p><a href="#" id="start">Start the operation</a>
</p>
<p id="result"></p>

CSS

img {
    margin-bottom: 40px;
    display: none;
}

JavaScript

$("#start").click(function (e) {
    e.preventDefault();

    var ajaxContainer = $('.spinner');
    
    // show
    $(ajaxContainer).show();

    // long operation
    var items = GetItems();
    $.each(items, function (index, value) {
        ProcessItem(value);
    });

    // hide
    //$(ajaxContainer).hide();

});

function GetItems() {
    return [1,2,3,4,5,6,7,8,9];
}

function ProcessItem(value) {
    
    for(var i = 0; i < 1000000; i++) {
        
    }
    
    console.log(value);
}