当前位置:首页 » HTML/CSS

连续点赞异步触发计数

2018-10-01 08:00 本站整理 浏览(44)

防止连续触发异步请求,2秒发送一次计数
  

 
    var view_count_num=0;
    var view_count_status=true;
    $('.journal-reward').on('click',  function(event) {
       view_count_num++;
       if(view_count_status){
           view_count_status = false;
           setTimeout(function(){
              $.ajax({
                url: '/articles/add_view_count',
                data: {id:<%= @article.id%>,view_count:view_count_num},
                type: "get",
                success: function(data){
                  view_count_status = true;
                  $("#view_count").html(data["view_count"]);
                }
              });
              view_count_num = 0;
           },2000);
        }
    });