阻止和QUOT;等待"在javascript函数? [英] Blocking "wait" function in javascript?

查看:109
本文介绍了阻止和QUOT;等待"在javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个JavaScript项目我工作的一部分,还有一些同步的Ajax调用的(我猜这使得它sjax,但我离题)的。我现在写一个调试面板,可以让我通过包装 $。阿贾克斯来测试出网站的一些人工模拟的网络条件。简单的事情:伪造一个500的响应等优点,使得Ajax调用需要更长的时间。

As part of a Javascript project I'm working on, there are some synchronous ajax calls (I guess that makes it "sjax", but I digress). I'm now writing a debugging panel which would allow me to test out the site with some artificially simulated network conditions by wrapping $.ajax. Simple things: faking a 500 response etc, and making the ajax calls take much longer.

对于异步调用,这很简单。当真实的反应回来,添加的setTimeout ,使其等待​​人工响应时间触发回调之前。但是,这不与同步调用工作明显,因为的setTimeout 不同步

For the asynchronous calls, it's simple. When the real response comes back, add a setTimeout to make it wait for the artificial response time before triggering the callback. However, this doesn't work with the synchronous calls obviously, since setTimeout isn't synchronous.

那么,有没有一种方法,使JavaScript程序执行一个阻塞等待的时间设定金额?

So, is there a way to make a Javascript program perform a blocking wait for a set amount of time?

我能想到的唯一的事情是这样的:

The only thing I could think of would be something like this:

function wait(ms) {
    var start = +(new Date());
    while (new Date() - start < ms);
}

有没有更好的解决办法?

Is there a better solution?

(另外,请假设有一个很好的理由阻止Ajax调用... : - \

推荐答案

不要在JavaScript的水平做到这一点。找一个代理,如提琴手并成立了一个的自动应答延迟一个时​​间段的通话。

Do not do it on the JavaScript level. Get a proxy such as Fiddler and set up an AutoResponder to delay the call by a time period.

这篇关于阻止和QUOT;等待&QUOT;在javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆