我遇到了函数调用的问题。附加的是我在Click事件上调用Test(),在Test()内部调用UpdateType()。由于某些原因,UpdateType运行两次。它第一次运行并完全跳过ajax调用(并返回" undefined"),然后立即再次运行并执行ajax调用。但是第二次,它没有完成" if(UpdateType(1))"声明。如果我第二次点击它,它会提醒" Huzzah!"。
HTML:
<h1 onclick="Test();">Package Designer</h1>
使用Javascript:
function UpdateType(Type)
{
var returnValue;
if (intCurrentType != Type)
{
$.ajax({
type: "POST",
url: "PackageDesigner.aspx/SetPackageType",
data: "{'strType' : '" + Type + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: "true",
cache: "false",
success: function (msg)
{
alert(msg.d);// **alerts "1"**
switch (msg.d)
{
case "-5": //Invalid ID
alert('You have been idle for too long');
window.location.replace("PackagesManager.aspx");
returnValue = false;
break;
case "-4": //invalid
alert('Not a Type.');
returnValue = false;
break;
case "-3": //Server Error
alert('There was an error with your request.');
returnValue = false;
break;
case "-2": //Not Authenticated
window.location.replace("Login.aspx?ReturnUrl=PackageDesigner.aspx");
returnValue = false;
break;
case "-1": //General Error
alert('There was an error with your request. Please try again.');
returnValue = false;
break;
default: //Success
intCurrentType = Type;
returnValue = true;
break;
}
},
Error: function (result)
{
alert('error');
}
});
return returnValue;
}
else
{
return true;
}
}
function Test()
{
if(UpdateType(1))
{
alert("Huzzah!!");
}
}
我尝试在空白网页中隔离代码,它仍然做同样的事情,所以我觉得我排除了它在网页上的其他问题。
感谢任何帮助。