我想执行一系列函数调用,每个函数调用都会在屏幕上发布一条 jGrowl 消息。当我立即执行此操作时,屏幕上不会显示任何内容。当我尝试在超时循环中设置延迟时,所有调用都会立即执行。
这是代码:
var tests = [];
tests.push("$.notification('this is a test');");
tests.push("$.notification('this is a test', 7);");
tests.push("$.notification({ message: 'this is a test', code: 'warning', sticky: false });");
tests.push("$.diagnostic({ message: 'click OK to continue', alert: true });");
tests.push("$.notification.erase();");
// 为什么这不起作用?//>
函数等待(毫秒){ var done = false; while (!done) { setTimeout(function () { done = true; }, ms) } }
这是控制台:
16.30.6.265: : Executing test[0]: $.notification('this is a test');
16.30.6.266: : Executing test[1]: $.notification('this is a test', 7);
16.30.6.266: : Executing test[2]: $.notification({ message: 'this is a test', code: 'warning', sticky: false });
16.30.6.266: : Executing test[3]: $.diagnostic({ message: 'click OK to continue', alert: true });
16.30.6.266: : Executing test[4]: $.notification.erase()
;
时间戳以毫秒为单位,因此不会发生延迟。