简体   繁体   English

当循环跟随语句时,为什么JS Alert在console.log之前执行

[英]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.

相关问题 为什么 alert(); 在 console.log() 之前运行; - Why does alert(); run before console.log(); 根文件中的console.log() 上面的导入语句在导入之前不会执行 - console.log() above import statements in root file does not execute before import console.log不在回调中执行,但是alert()可以吗? - console.log doesn't execute within callback but alert() does? 为什么即使在console语句之后更新了数组,console.log为什么也打印数组? - Why does console.log prints array even when the array is updated after the console statements? Javascript:为什么有时 alert() 不起作用但 console.log() 起作用? - Javascript: Why sometimes alert() does not work but console.log() does? 为什么返回带警报的false而不是console.log - Why does return false work with alert but not console.log 为什么逗号在console.log中起作用而不发出警报? - Why does a comma work inside a console.log and not alert? 为什么在Greasemonkey示例中为此jQuery触发alert()但不触发console.log()? - Why does alert() fire but not console.log() for this jQuery in Greasemonkey example? 为什么添加console.log语句会阻止测试超时? - Why does adding console.log statements prevent test timeouts? 为什么console.log在分配之前会记录一个值? - Why does console.log log a value before it's assigned?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM