[英]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. 我有一个JS代码段,可用来在两次调用之间引入暂停。 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. 现在我看到的是,如果在while循环之前向此函数添加警报,它将立即显示警报,然后运行while循环,但是如果我添加console.log而不是alert,console.log将在while之后回显数据循环完成。 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: 使用jquery可以将ajax调用临时设置为同步:
$.ajaxSetup({ "async": false });
// make synchornous ajax call
var jqHxr = $.get(....)
$.ajaxSetup({"async": true});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.