如何在ajax post setTimeout reload之后保持数据id被点击

时间:2017-10-28 16:27:44

标签: jquery ajax

如何在ajax post setoutout reload后点击id标签?

HTML

<a id="star" class="toto">sort by star</a>
<a id="date" class="toto">sort by date</a>
<div id="online">
  <!-- SQL_online.php ROW LOAD HERE -->
</div>

JQUERY / AJAX

$(document).ready(function(){
     load_data();

     function load_data(query)
     {
      $.ajax({
       url:"SQL_online.php",
       method:"POST",
       data:{query:query},
       success:function(data)
       {
       $('#online').html(data);
       }
      });
    	setTimeout(function(){load_data()}, 10000); /// refresh each 10s
     }

     $('.toto').click(function(){
      var search = $(this).attr('id');
      console.log (search);
      if(search != '')
      {
       load_data(search);
      }
      else
      {
       load_data();
      }
     });

    });

当ajax重新加载SQL时,我失去了“排序依据”选择。如何保持点击“a”选择ID?谢谢!

1 个答案:

答案 0 :(得分:0)

我认为这可以帮助您每10秒刷新一次数据并且查询仍然有效。我不确定这是你想要的!

$(document).ready(function(){
 window._intervalId = setInterval(load_data(), 10000);

 function load_data(query)
 {
  $.ajax({
   url:"SQL_online.php",
   method:"POST",
   data:{query:query},
   success:function(data)
   {
   $('#online').html(data);
   }
  });

 }

 $('.toto').click(function(){
  if (window._intervalId !== undefined){
  clearInterval(window._intervalId);
  }
  var search = $(this).attr('id');
  window._intervalId = setInterval(load_data(search), 10000); /// refresh each 10s
 });

});

请告诉我这是否是您要找的......所以我可以编辑我的答案。