如何在发布或循环时在JavaScript中编写异步方法? [英] How can I write an async method in JavaScript when posting or looping?

查看:131
本文介绍了如何在发布或循环时在JavaScript中编写异步方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的JavaScript代码中创建了一个循环,在那个循环中我使用带有回调方法的XMLHttpRequest,但我遇到了问题。

I have made a loop in my JavaScript code and in that loop I use an XMLHttpRequest with a callback method but I'm having a problem with it.

我的循环结束了回调执行。这不好,我需要在调用函数后执行回调,而不是在循环之后执行。我怎么能这样做?

When my loop ends the callback executes. This is not good, I need the callback to execute after calling a function, not after looping. How can I do this?

博客上有人写道我可以使用异步方法。如何在这种情况下使用异步方法?

Somebody on a blog wrote that I can use an async method. How can I use an async method in this condition?

for (var i = 0; i < stuff.length; i++) {
                        var exist = IsstuffExist(stuff[i]);
                        alert(exist);
                    }

被叫函数show alert();

the called function show alert();

当我调用函数时,我首先得到了我的代码警报,回调警报出现在我的代码之后。

when i call function then i got alert of my code first and callback alert comes after my code.

我怎么能停止循环一段时间然后我可以回复来自被调用函数的回调

how i can stop loop for sometime then i can get back response as callback from called function

如果我在循环中使用

 IsstuffExist(tags[i], function (result) {
                            if (result == true) {
                                $("#txtstuff").append(stuff[i]);
                            }


推荐答案

你基本上必须改变你的想法。你需要改变你的风格,编写如下代码:

You basically have to change your thinking. You need to change your style from writing code like this:

for (var i = 0; i < stuff.length; i++) {
    var exist = IsstuffExist(stuff[i]);
    alert(exist);
}

写这样的东西:

for (var i = 0; i < stuff.length; i++) {
    IsstuffExist(stuff[i], function(exist) {
        alert(exist);
    });
}

IsstuffExist 功能可写成这样:

function IsstuffExist (stuff, callback) {
    // Do things and once you get the `exist` variable you can
    // pass it to the callback function. Any code that needs
    // to continue processing things can then resume from
    // within the callback function:
    callback(exist);
}

这种技术可以嵌套,将回调函数传递给其他回调函数。一个具体的例子是ajax调用:

This technique can be nested, passing the callback function to other callback functions. A concrete example is with ajax calls:

// I'm using my own ajax library in this example but it's the same
// if you use other libraries:

function IsstuffExist (stuff, mycallback) {
    // Make ajax call to find out if stuff exist:
    ajax('some/url.com', {
        callback : function (r) {
            var status = r.responseText;
            mycallback(status);
        }
    });
}

注意:我将函数的回调重命名为 mycallback 以避免混淆。但在实际代码中,我只需将其命名为 callback

note: I renamed the callback for the function to mycallback to avoid confusion. But in real code I would simply name it callback.

所以现在代替编写代码这个:

So now instead of writing code like this:

for (var i = 0; i < stuff.length; i++) {
    if(IsstuffExist(stuff[i])) {
        doSomethingAndUpdateTheHTML();
    }
}

你这样写:

for (var i = 0; i < stuff.length; i++) {
    IsstuffExist(stuff[i],function(exist){
        if (exist) {
            doSomethingAndUpdateTheHTML();
        }
    });
}

这篇关于如何在发布或循环时在JavaScript中编写异步方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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