$ .get在循环中:为什么函数在递增后执行? [英] $.get in a loop: why the function is performed after incrementing?

查看:287
本文介绍了$ .get在循环中:为什么函数在递增后执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Javascript的初学者,我觉得我的关于$ .get jQuery有些问题。



通常,您可以将它分配给一个函数这将在数​​据正确检索后执行。



但是,如果我将$ .get放在一个循环中,即使尚未检索到数据,循环也会继续执行,这是我的问题。



这是我的代码(这是GreaseMonkey的):

  var1 = document.getElementsByClassName(some_class); 
i = 0;

while(i< var1.length){
url = var1 [i] .getElementsByTagName(some_tag)[0] .href;

$ .get(url,function(data){
if(data.contains(some_string)){
alert(i);
}
});
i ++;
}

这里,如果alert返回1,则返回var1.length事件。



我试着在url声明后面加上alert(i),并且我明白i ++是在我的$ .get中的函数之前完成的。



这肯定是一个小问题,但我无法理解这种情况的发生。

$ b

 (function(i){
$ .get(url,function(data){
if(data.contains(some_string)){
alert(i);
}
});
})(i);

立即调用的函数表达式会导致当前值 i 在外部范围中,通过函数的参数 i (然后隐藏外部变量)进行绑定。如果你喜欢,给函数参数一个不同的名字。



请注意,这只能解决您实际说明的问题,即循环变量独立于回调而递增。如果您希望确保AJAX请求一次运行一个,那么还有其他解决方案,例如:

  var els = document .getElementsByClassName( some_class); 
var i = 0;

(function loop(){
if(i< els.length){
var el = els [i];
var url = el.getElementsByTagName (some_tag)[0] .href;
$ .get(url).done(function(data){
if(data.contains(some_string)){
alert (i);
}
i ++;
},loop); // .done(f1,f2) - see below
}
})();

.done()调用位于形式为 .done(callback,loop),这两个函数将按顺序调用。因此, i ++ 行总是首先发生,然后然后它会安排 loop 被调用伪递归地处理下一个元素。


I am a beginner in Javascript and I feel that there is something wrong with me about the $.get jQuery.

Normally, you can assign it to a function that will execute after the data is retrieved correctly.

But if I put my $.get in a loop, the loop continues to execute even if the data is not yet retrieved, and here is my problem.

Here is my code (this is for GreaseMonkey):

var1 = document.getElementsByClassName("some_class");
i = 0;

while (i < var1.length) {
    url = var1[i].getElementsByTagName("some_tag")[0].href;

    $.get(url, function(data) {
        if (data.contains("some_string")) {
            alert(i);
        }
    });
i++;
}

Here, the alert returns var1.length event if it should returns 1 for exemple.

I try to put an alert(i) just after the url declaration and I understood that i++ was done before the function in my $.get.

This is surely a trivial problem, but I can not grasp the logic to not make this happen.

解决方案

Wrap your $.get function thus:

(function(i) {
    $.get(url, function(data) {
        if (data.contains("some_string")) {
            alert(i);
        }
    });
})(i);

The immediately invoked function expression causes the current value of i that's in the outer scope to be bound via the function's parameter i (which then hides the outer variable). If you like, give the function parameter a different name.

Note that this only fixes the problem you actually stated, which is that the loop variable is incremented independently of the callbacks. If you wish to ensure that the AJAX requests run one at a time then there are other solutions, e.g.:

var els = document.getElementsByClassName("some_class");
var i = 0;

(function loop() {
    if (i < els.length) {
        var el = els[i];
        var url = el.getElementsByTagName("some_tag")[0].href;
        $.get(url).done(function(data) {
            if (data.contains("some_string")) {
                alert(i);
            }
            i++;
        }, loop);   // .done(f1, f2) - see below
     }
})();

The .done() call is in the form .done(callback, loop) and the two functions will be called in order. So the i++ line always happens first, and then it arranges for loop to be called pseudo-recursively to process the next element.

这篇关于$ .get在循环中:为什么函数在递增后执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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