另一个功能完成后如何立即触发功能?

时间:2016-06-08 18:49:55

标签: javascript jquery ajax

我有以下代码:

<script>
function refreshChat() {
    var id = "'.$convers_id.'";
    var receiver = "'.$system->getFirstName($second_user->full_name).'";
    $.get("'.$system->getDomain().'/ajax/refreshChat.php?id="+id+"&receiver="+receiver, function(data) {
        $(".conversation-content").html(data);
    });
    var scroller = $(".conversation-message-list").getNiceScroll(0);
    $(".conversation-message-list").getNiceScroll(0).doScrollTop($(".conversation-content").height(),-1); 
}
window.setInterval(function(){
    refreshChat();
}, 2000);

function sendMessage() {
    var user2 = "'.$user2.'";
    var message = $("#message");
    if(message.val() != "" && message.val() != " ") {
        $.get("'.$system->getDomain().'/ajax/sendMessage.php?id="+user2+"&msg="+encodeURIComponent(message.val()), function(data) {
        $(".conversation-content").html(data);
            message.val("");
        });
    }
}
$(document).keypress(function(e) {
    if(e.which == 13) {
        sendMessage();
    }
});
</script>

现在,refreshChat函数每2秒调用一次ajax脚本。当您输入消息并按Enter键时,它会调用另一个ajax脚本。我想要它做的是同时调用两个函数。因此脚本首先调用sendMessage函数,然后刷新。

我该怎么做?我已经尝试将其更改为:

<script>
function refreshChat() {
    var id = "'.$convers_id.'";
    var receiver = "'.$system->getFirstName($second_user->full_name).'";
    $.get("'.$system->getDomain().'/ajax/refreshChat.php?id="+id+"&receiver="+receiver, function(data) {
        $(".conversation-content").html(data);
    });
    var scroller = $(".conversation-message-list").getNiceScroll(0);
    $(".conversation-message-list").getNiceScroll(0).doScrollTop($(".conversation-content").height(),-1); 
}

function sendMessage() {
    var user2 = "'.$user2.'";
    var message = $("#message");
    if(message.val() != "" && message.val() != " ") {
        $.get("'.$system->getDomain().'/ajax/sendMessage.php?id="+user2+"&msg="+encodeURIComponent(message.val()), function(data) {
            $(".conversation-content").html(data);
            message.val("");
        });
    }
}
$(document).keypress(function(e) {
    if(e.which == 13) {
        sendMessage();refreshChat();
    }
});
</script>

但是这只是首先输入消息,它只在第二次按键(输入)时刷新。我想事先感谢大家帮助我。

3 个答案:

答案 0 :(得分:2)

这实际上是一种错觉。两个函数都被调用,但聊天窗口正在刷新之前聊天消息能够保存它们。

要解决此问题,只有在成功保存新邮件后才应刷新聊天窗口:

function refreshChat() {
    // Removed for brevity
}

function sendMessage() {
    var user2 = "'.$user2.'";
    var message = $("#message");
    if(message.val() != "" && message.val() != " ") {
        $.get("'.$system->getDomain().'/ajax/sendMessage.php?id="+user2+"&msg="+encodeURIComponent(message.val()), function(data) {
            $(".conversation-content").html(data);
            message.val("");

            // Now, this will only be called once the ajax is complete
            refreshChat();
        });
    }
}

$(document).keypress(function(e) {
    if(e.which == 13) {
        sendMessage();

        // I removed the refreshChat() call from here and moved it
        // into the $.get() callback above ^^
    }
});

正如您所看到的,我已将您的refreshChat()方法从jQuery $.get()回调中调出。

答案 1 :(得分:0)

您是否尝试过使用回调,这可能是您需要的?

以下是供参考的链接。

http://www.impressivewebs.com/callback-functions-javascript/

答案 2 :(得分:0)

我的工作奖

考虑到我的要求,我已将Wes Foster的awnser标记为正确。让它起作用的是在get函数之后应用promise。这样,ajax脚本会根据需要调用两次。我希望将来可以帮助某人。 (看着我......穿越时空......)你会在下面找到我的代码:

function refreshChat() {
var id = "'.$convers_id.'";
var receiver = "'.$system->getFirstName($second_user->full_name).'";
$.get("'.$system->getDomain().'/ajax/refreshChat.php?id="+id+"&receiver="+receiver, function(data) {
    $(".conversation-content").html(data);
});
var scroller = $(".conversation-message-list").getNiceScroll(0);
$(".conversation-message-list").getNiceScroll(0).doScrollTop($(".conversation-content").height(),-1); 
}

function sendMessage() {
    var user2 = "'.$user2.'";
    var message = $("#message");
    if(message.val() != "" && message.val() != " ") {
        $.get("'.$system->getDomain().'/ajax/sendMessage.php?id="+user2+"&msg="+encodeURIComponent(message.val()), function(data) {
            $(".conversation-content").html(data);
            message.val("");


            refreshChat();
        }).done(refreshChat);
}
}
$(document).keypress(function(e) {
    if(e.which == 13) {
        sendMessage();


    }
});