duanji1026 2015-01-02 13:10
浏览 37
已采纳

如何在按键的同时停止通过aJax重新发送脚本?

I have a script which stores message into DB when enter key is pressed.

<textarea class="comment" name="comment" id="comment" onKeyPress="return checkSubmit(event)" onKeyDown="return checkTypingStatus(event)" >Comment/Reply</textarea>

function checkSubmit(e)
    {

       if(e && e.keyCode === 13)
       {

                var sender = <?=$_REQUEST['sender'];?>;
                var user_id = <?=$_SESSION['userid'];?>;
                var cid = <?=$_REQUEST['cid'];?>;

                var comment = $("#comment").val();

                comment = encodeURIComponent(comment);

                var dataString = 'sender='+ sender + '&user_id=' + user_id + '&comment=' + comment + '&cid=' + cid;

                        if(comment=='' || comment=='Comment%2FReply')
                         {
                            alert('Please Give some text into message box.');
                         }
                    else
                        {
                        $("#flash").show();
                        $("#flash").fadeIn(400).html('<img src="images/ajax-loading.gif" align="absmiddle">&nbsp;<span class="loading">Sending Message...</span>');
                    $.ajax({
                                type: "POST",
                                url: "ajax_comment.php",
                                data: dataString,
                                cache: false,
                                success: function(html){

                                        $("p#update").append(html);
                                        $("p#update").fadeIn("slow");
                                        document.getElementById('comment').value='';
                                        $("#comment").focus();


                                        $("#flash").hide();
                                }
                            });
                        }       
                        return false;

       }
    }

Everything is working okay except if user keeps pressing enter key for long time the same message is stored multiple times until the enter is released.

I have tried the same script with onKeyUp event. But the script does not work as it should be.

  • 写回答

1条回答 默认 最新

  • dongpan1308 2015-01-02 13:13
    关注

    Just put a lock to disable the ajax request until the last one has finished:

    var lock = false;
    function checkSubmit(e)
        {
    
           if(e && e.keyCode === 13 && !lock)
           {
            lock = true;
            ....
            $.ajax({
            ....
            success: function(html){
                 lock = false;
            ...
    

    Another option could be disabling the textarea and checking if it's disabled or not instead of using the lock but I think you got the idea with this example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP