jQuery的Ajax调用不执行for循环 [英] Jquery Ajax call not executed in for loop

查看:499
本文介绍了jQuery的Ajax调用不执行for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用这个函数

pre $函数readPinMode(callback,pin){
$ .ajax({
类型:'GET',
url:path,
data:{
'funct':readPinMode,//包含函数和循环工作
'pin' :pin,
'php':0
},
成功:函数(结果){
//console.log(result);
回调(结果) ;
},
error:function(xhr,textStatus,error){
console.log(xhr);
console.log(textStatus);
console。日志(错误);
}
});
};

以这种方式,根本不会做任何事情:

  $(document).ready(function(){
<?php
$ js_array = json_encode($ GLOBALS ['gpio']); //从包含文件中获取
echovar pins =。$ js_array。; \\\
;
?>
console.log(Document ready。) ;
for(i = 0; i var mode =m+ pins [i];
函数initMode(){
readPinMode(function(ret){
console.log(ret);
$(mode).text(ret);
console.log(mode);
}它将进入for循环();
;

$ (我可以登录控制台模式 pins [i] ,他们正在工作),但功能似乎不是
控制台不显示任何东西。

有没有办法解决这个问题?
谢谢

没有什么w使用回调函数,但是你确实错过了一些事情。你忘记调用你的 initMode 函数, mode 应该是你提到的ID:

 < script> 
函数readPinMode(callback,pin){
$ .ajax({
类型:'GET',
url:'SGWEB / header.php',
data :{
'funct':readPinMode,//包含的函数和工作循环
'pin':pin,
'php':0
},
成功:函数(结果){
回调(结果);
},
错误:函数(xhr,textStatus,错误){
console.log(error);
}
});


函数initMode(mode,pin){
readPinMode(function(ret){
$(mode).text(ret);
pin);


$(document).ready(function(){
var pins =<?= json_encode($ GLOBALS ['gpio'])?>
for(i = 0; i var mode =#m+ pins [i];
initMode(mode,pins [i]);
}
});
< / script>

以下是 FIDDLE 我创建的,以便您可以看到它是如何工作的。


Using this function

  function readPinMode(callback,pin){
     $.ajax({
        type: 'GET',
        url: path,
        data: {
        'funct': "readPinMode", //function included and working ou of loops
        'pin': pin,
        'php': 0
     },
     success: function (result) {
        //console.log(result);
        callback(result);
     },
        error: function (xhr, textStatus, error) {
        console.log(xhr);
        console.log(textStatus);
        console.log(error);
    }
   });
 };

in this way, simply does not do nothing:

      $( document ).ready(function() {
  <?php
  $js_array = json_encode($GLOBALS['gpio']); // got from included file, working
  echo "var pins = ". $js_array . ";\n";
  ?>
  console.log( "Document ready." );
  for (i = 0; i < pins.length; i++) {
        var mode = "m" + pins[i];
        function initMode(){
        readPinMode(function(ret){
        console.log(ret);
        $(mode).text(ret);
        console.log(mode);
      }, pins[i]);
    };
  }

It enters the for loop (I can log in console mode and pins[i], they are working) but the function seems to not be called. Console does not show anything.

Is there a way to solve this? Thanks

解决方案

There's nothing wrong with using callback functions, however you did miss a couple of things. You are forgetting to call your initMode function and mode should be an ID like you mentioned:

<script>
    function readPinMode(callback, pin) {
        $.ajax({
            type: 'GET',
            url: 'SGWEB/header.php',
            data: {
                'funct': "readPinMode", //function included and working ou of loops
                'pin': pin,
                'php': 0
            },
            success: function (result) {
                callback(result);
            },
            error: function (xhr, textStatus, error) {
                console.log(error);
            }
        });
    }

    function initMode(mode, pin) {
        readPinMode(function (ret) {
            $(mode).text(ret);
        }, pin);
    }

    $(document).ready(function () {
        var pins = <?= json_encode($GLOBALS['gpio']) ?>;
        for (i = 0; i < pins.length; i++) {
            var mode = "#m" + pins[i];
            initMode(mode, pins[i]);
        }
    });
</script>

Here's a FIDDLE I created so that you can see how it works.

这篇关于jQuery的Ajax调用不执行for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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