Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向匿名Javascript函数传递参数_Javascript_Xmlhttprequest_Dom Events_Anonymous Methods - Fatal编程技术网

向匿名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中的控制台登录解决了这个问题!

认为这很简单。可能是“考虑下面的代码”触发了它。代码应该遵循简单的英语解释,而不是之前的解释。这里的大多数用户都不喜欢“修复我的代码”风格的问题,而这个问题一开始看起来很像。