Online_ChatRomm

 $(document).ready(function() {
        var timeid = null;
        timeid = setInterval('counter()', 5000);
    }).keydown(function(event) {
    var key = window.event ? event.keyCode : event.which;
    if ((key.toString() == "13") && (event.ctrlKey)) {
        sendMsg();
    }
    });
    function counter() {
        $.ajax({
            url: "BaseAjaxAppliction/newmsg.ashx",
            type: "get",
            dataType: "html",
            data: "myId=<%= MyId %>&friendId=<%= FriendId %>",
            success: function(msg) {
                if (msg != "0") {
                    $("#body").append(msg);
                }
            }
        });
        setTimeout('scrollDiv()', 100);
    }
    function sendMsg() {
        if ($("#content").val() == "") {
            $("#error").html("<span style=\"font-color:red;\">发送消息不能为空!</span>");
            return false;
        }
        $.get("BaseAjaxAppliction/newmsg.ashx", { body: $("#content").val(), myId: "<%= MyId %>", friendId: "<%= FriendId %>" },
                function(msg) {
                    if (msg == "1") {
                        var de = new Date();
                        $("#body").append("<ul><li>我  " + de.toLocaleString() + "</li><li>说:" + $("#content").val() + "</li></ul>");
                        $("#content").val("");
                    }
                    else {
                        $("#error").html("<span style=\"color:red;\">消息发送失败,请重发!</span>");
                        $("#content").val("");
                    }
                });
                setTimeout('scrollDiv()', 100);
    }
    function scrollDiv(){
        $("#bodys").scrollTop($("#body").height());
    }
原文地址:https://www.cnblogs.com/Andon_liu/p/1340642.html