switch ajax,在ajax完成之前运行的switch语句

问题:

在ajax执行完成功能之前,有没有办法暂停javascript?

相关代码:

function validateinput (name, parent, access)

{

//we assume that the input does not exist

exists = false;

//validate with server, expected response is true or false

$.post("AJAXURL", {action: "category", name: name, parent: parent},

function (data, textStatus, xhr)

{

//if returned true, the entry does exist for that parent

if (data == "true")

{

alert("AJAX");

exists = true;

}

else

exists = false;

});

switch (true)

{

//If the name field is blank (still displaying hint)

case (name == 'Subcategory Name' || name == 'Category Name'):

alert("Please enter a name");

break;

//if the name already exists for that parent

/*****/ case (exists == true):

alert("SWITCH");

break;

//if the parent field is displaying hint, assume parent is blank

case (parent == 'Parent Category'):

parent = '';

break;

//if the usergroup field is blank, assume value of parent

case (access == 'UserGroup Allowed To View'):

//if parent is also blank, assume lowest level usergroup access

if (parent == '')

access = "UserGroupTicketWorkers";

else

$.post("AJAXURL", {action: "specificcat", name: parent},

function (data, textStatus, xhr)

{

access = data['access'];

}

);

break;

default:

return true;

}

return false;

}

问题的详细说明:

用户点击页面中的链接即可启动此功能。

函数将返回true或false,无论用户输入是否有错误

当这段代码执行时,我会在收到alert("SWITCH");之前收到alert("AJAX");(第二种情况的条件当前是正确的,我在调试中将其反转以弄清楚发生了什么)

我有一个临时修复它,它在默认情况下直接移动第二个案例,但我猜测更快的计算机可以比服务器提供响应更快地执行交换机比较,因此不是永久解决方案。

我知道我没有从ajax调用中获取新值,因为有时间与服务器通信,因此,我首先想到的是找到一种暂停函数的方法,直到ajax成功函数为止完成。

我无法将开关置于ajax调用中,因为此函数需要返回一个值。

分辨率

我使用了异步设置,它就像一个魅力。只需将我的代码从$.post()更改为$.ajax()并将其设置为发布

$.ajax(

{

url: "plugins/actions/edittickets/libs/changetickets.php",

type: 'POST',

data:

{

action: "category",

name: name,

parent: parent

},

async: false,

success: function (data, textStatus, xhr)

{

//if returned true, the entry does exist for that parent

if (data == "true")

exists = true;

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值