I need to have a delay in my JavaScript code, but I'm having problems if I create a timeout function in my code. However, if I take out the timeout code it works perfectly fine. I searched through the other post in here about timeout/delay but my case is a little bit different I think.
var myArray = new Array('Book One', 'Book Two', 'Book Three', 'Book Four');
x = myArray.length - 1;
(function myLoop(x) {
    page = 3;
    (function myLoop2(page) {
        //setTimeout(function () {   
        var name = myArray[x];
        alert(name + ' Page: ' + page);
        if (--page) myLoop2(page);
        //}, 1000 )
    })(page);
    if (x != 0) myLoop(--x);
})(x);
If I remove the comment in the code, it will give me a different output.
 
     
     
    