0

内部関数関数の使用に問題があります。

this.init = function () {

  var size = this.ref;
  var wall = this.element;
  var id = this.id;
  var initRef = this.init;

  this.update(id, size, wall, initRef);
}


this.update = function (id, size, wall, init) {

  $.get(url, "cpart=" + id + "&ref=" + size, (function (wall, size, init) {
    return function (data) {
      if (data) {
        var response = JSON.parse(data);
        size = response["psize"];
        wall.append(response["msg"]);
        wall.scrollTop($(document).height());
      }

      init();
    }
  })(wall, size, init));
}

私が抱えている問題は 2 回目の繰り返しです。ajax リクエストの変数は定義されておらず、なぜそれが起こるのかわかりません。関数が最初に呼び出されたときは機能しますが、2 回目は変数とサイズが定義されていません。

事前に助けてくれてありがとう

4

2 に答える 2

0

getステートメントの形式は次のようになると思います:

$.get(url, {cpart: id, ref: size}, (function (wall, size, init) {
   // existing stuff
});

これが役立つことを願っています。

于 2012-09-12T18:39:44.373 に答える
0

代わりにこれを試してください:

this.update = function (id, size, wall, init) {

    $.get(url, "cpart=" + id + "&ref=" + size, (function (self, wall, size, init) {
        return function (data) {
            if (data) {
                var response = JSON.parse(data);
                size = response["psize"];
                wall.append(response["msg"]);
                wall.scrollTop($(document).height());
            }

            init.apply(self);
        }
    })(this, wall, size, init));
}

実際にアクティブ化オブジェクトを指定せずに init を呼び出しているため、何かが起こっている可能性があります。


更新: 私は今、あなたのコードをより注意深く読んでいます。

あなたが達成しようとしていることは完全にはわかりませんが、改訂版は次のとおりです。

this.update = function () {
    var self = this;

    $.get(url, "cpart=" + id + "&ref=" + size, function(data) {
        if (data) {
            var response = JSON.parse(data);
            self.size = response["psize"];
            self.wall.append(response["msg"]);
            self.wall.scrollTop($(document).height());
        }

        init.call(self);
    });
}

に引数を渡すのではなくupdate、オブジェクトのプロパティを直接使用していることに注意してください。object への参照を変数に保持します。これは、それを囲む関数 (つまり、「更新」関数) で宣言されているため、指定しselfた無名関数からアクセスできます。$.get()


更新 2

init を呼び出すと update が呼び出され、init が再度呼び出されます。その悪循環を断ち切る方法があると思いませんか?
サーバーとユーザーのブラウザの両方に打撃を与えることになります。

何を達成しようとしているのかを教えていただければ、それが最善だと思います。


アップデート 3

私はあなたのためにあなたの仕事をしているように感じます:J

// If you're writing a "class", there's got
// to be a constructor somewhere:

function YourClass(id, ref, element) {
    // These need to come from somewhere...
    this.id = id;
    this.ref = ref; 
    this.element = element;
}


// Now we set your "class methods" on YourClass.prototype,
// so they can be shared among all the instances of YourClass.
// Create instances like this: 
// obj = new YourClass();

YourClass.prototype.init = function() {
    // You want to give these properties
    // alternate names, I'll respect that.
    // (notice obj.ref won't ever be updated, but obj.size will)
    this.size = this.ref;
    this.wall = this.element;
    this.update();
}


YourClass.prototype.updateFromData = function(data) {
    // I moved this code to a helper "class method" to make things more clear
    if (data) {
        var response = JSON.parse(data);
        this.size = response["psize"];
        this.wall.append(response["msg"]);
        obj.wall.scrollTop($(document).height());
    }
    this.init();
}


YourClass.prototype.update = function() {
    // Not the most elegant way of coding this,
    // but it should be easier to read.        
    function createUpdater(obj){
        return function(data){
            obj.updateFromData(data);
        }
    }        
    $.get(url, "cpart=" + this.id + "&ref=" + this.size, createUpdater(this));        
}

// An alternative to the above would simply be this:
// YourClass.prototype.update = function() {
//     $.get(url, "cpart=" + this.id + "&ref=" + this.size, this.updateFromData.bind(this));
// }
于 2012-09-12T18:41:24.930 に答える