在jQuery挂起的用户操作中保存ajax请求的方法

时间:2012-03-20 16:17:17

标签: jquery ajax

下面的脚本是专门的文本编辑器的一部分,效果很好。它会在' blur'。

之后发送一个ajax请求

因为它频繁地与数据库联系,所以我们想要编译这些请求,然后当用户点击“保存”时,通过ajax一次性发送这些请求。按钮。

我想知道jQuery中是否存在类似这样的标准化方法?

$(document).ready(function() {

  $("#resume_holder").ready(function () {

  $('#resume_holder').contents().find('[contenteditable]').on("focus", function(e) {
      var $this = $(this);
      $this.data('before', $this.html());

  }).on("blur", function(e) {

     var $this = $(this);
     var before = $this.data('before');
     var txt = $(e.target).closest("section").html();
     var id = $(e.target).closest("section").attr('id');

                $.ajax({
                        type : 'POST',
                        url : '<?php echo site_url('resume/edit_resume_ajax'); ?>',
                        dataType: "html",
                        context : $(this),
                        data: {
                            edit_id: id,                                
                            edit_value: txt,
                            edit_before: before
                        },
                        success : function(msg){
                        console.log(msg);
                        },
                        error: function(msg){
                        console.log(msg);
                        }
                    }); 
                }); 
            });

 });    
    </script>

1 个答案:

答案 0 :(得分:1)

对我来说,选项是:

  1. HTML5本地存储。 如果需要保留请求队列/数据,即使页面被刷新/更改,那么我将使用HTML5本地存储。请参阅here或进行搜索。

  2. 如果是列表/数据数组,只需将其存储在Javascript变量中,直到单击保存按钮,如果在页面请求后数据不需要保留。

  3. 不是执行ajax调用onblur(),而是检测onblur()并记录上一个onblur()之间的时间段,如果超过X分钟则执行保存ajax请求。