简体   繁体   中英

Why does JS alert execute before console.log when a loop is following the statements

I have a JS code snippet which I use to introduce pause between calls being made. The snippet is:

function intPause(_wg_time_msec) {
    var _wg_now = new Date();
    var _wg_expire = _wg_now.getTime() + _wg_time_msec;
    while(_wg_now.getTime() < _wg_expire)
        _wg_now = new Date();
}

Now what I see is that if add an alert to this function before the while loop, it shows the alert instantly and then runs the while loop, but if I add a console.log instead of alert, console.log echoes the data after while loop completes. Can someone please help me in understanding this behavior and how can I get a better way to introduce pause between calls.

using jquery you can temporarily set ajax calls to be synchronous:

$.ajaxSetup({ "async": false });
// make synchornous ajax call
var jqHxr = $.get(....)
$.ajaxSetup({"async": true});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM