我应该在哪里放置超时以自动刷新页面?

时间:2019-07-04 08:46:34

标签: jquery ajax

我正在尝试自动刷新页面,以便可以从数据库中检索最新数据。

我希望它将不显示任何内容而显示最新数据,但是当我尝试输入新数据时仍然不会显示

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

  function load_data(query) {
    $.ajax({
      url: "php/checkinhelper.php",
      method: "post",
      data: {
        query: query
      },
      success: function(data) {
        $('#result').html(data);
      }
    });
    window.setTimeout(100);
  }

  $('#search_text').change(function() {
    var search = $(this).val();
    if (search != '') {
      load_data(search);
    } else {
      window.setTimeout(100);
      load_data();
    }
  });
});

2 个答案:

答案 0 :(得分:0)

        $(document).ready(function() {
        var elem = $('#search_text');

         setTimeout(() =>  elem.trigger('change'), 100);

          function load_data(query) {
            $.ajax({
              url: "php/checkinhelper.php",
              method: "post",
              data: {
                query: query
              },
              success: (data) => {
                  $('#result').html(data);
                  setTimeout(() =>  elem.trigger('change'), 100);
              }
            });
          }

          elem.change(function() {
            var search = $(this).val();
            if (search != '') {
              load_data(search);
            } 
          });
        });

答案 1 :(得分:0)

使用此选项:

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

     function load_data(query) {
$.ajax({
  url: "php/checkinhelper.php",
  method: "post",
  data: {
    query: query
  },
  success: function(data) {
    $('#result').html(data);
  }
});

}

setTimeout(function(){ load_data() }, 100);

$('#search_text').change(function() {
var search = $(this).val();
if (search != '') {
  load_data(search);
} else {
  window.setTimeout(100);
  load_data();
}
});
});
相关问题