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

时间:2012-12-06 20:42:40

标签: javascript ajax performance switch-statement

问题:

在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;
    }
}); 

2 个答案:

答案 0 :(得分:1)

尝试返回exists,如下所示。

function validateinput (name, parent, access)
{
    //we assume that the input does not exist
    var 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:
                exists = true;
        }
    });
    return exists;
}

答案 1 :(得分:0)

如果您在参数列表中添加'async':false,那么您可以通过同步方式发送AJAX查询。

有关详细信息,请参阅文档:http://api.jquery.com/jQuery.ajax/

但正如其他人在评论中指出的那样,这不是切换工作的方式,因此你仍然需要理解switch语句并更改相应的代码。有关详细信息,请参阅此处:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/switch