向匿名Javascript函数传递参数
考虑以下代码:向匿名Javascript函数传递参数,javascript,xmlhttprequest,dom-events,anonymous-methods,Javascript,Xmlhttprequest,Dom Events,Anonymous Methods,考虑以下代码: this.usedIds = 0; this.SendData = function(data) { var id = this.usedIds; this.usedIds++; this.xmlHttpThing.open("POST", "/Upload.aspx", true); this.xmlHttpThing.setRequestHeader("Content-type",
this.usedIds = 0;
this.SendData = function(data)
{
var id = this.usedIds;
this.usedIds++;
this.xmlHttpThing.open("POST", "/Upload.aspx", true);
this.xmlHttpThing.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var currentObject = this;
this.xmlHttpThing.onreadystatechange = function() { currentObject.UploadComplete(id) };
this.xmlHttpThing.send(data);
};
this.UploadComplete = function(id)
{
if (this.xmlHttpThing.readyState == 4)
{
//First time id is 0
//Second time id is 0 <<<--------- Why??
//Third time id is 1
//Fourth time id is 2
//Fifth time id is 3 ....
this.SendData("blabla");
}
};
this.usedIds=0;
this.SendData=函数(数据)
{
var id=this.usedIds;
这个.usedIds++;
this.xmlHttpThing.open(“POST”,“/Upload.aspx”,true);
这个.xmlHttpThing.setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
var currentObject=此;
this.xmlHttpThing.onreadystatechange=function(){currentObject.UploadComplete(id)};
this.xmlHttpThing.send(数据);
};
this.UploadComplete=函数(id)
{
if(this.xmlHttpThing.readyState==4)
{
//第一次id为0
//第二次id为0我过去也遇到过同样的问题。我不记得是什么导致了它,但我通过将增量移动到响应处理程序来修复它
this.UploadComplete = function(id)
{
if (this.xmlHttpThing.readyState == 4)
{
this.usedIDs++;
this.SendData("blabla");
}
};
编辑:经过思考,我的案例可能与你的不同。为什么不在分配id
时增加这个。使用DIDS
var id = this.usedIDs++;
禁用Firebug中的控制台登录解决了这个问题!认为这很简单。可能是“考虑下面的代码”触发了它。代码应该遵循简单的英语解释,而不是之前的解释。这里的大多数用户都不喜欢“修复我的代码”风格的问题,而这个问题一开始看起来很像。