代わりにこれを試してください:
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));
// }