Delay load of data for 2 seconds in jQuery ajax

两盒软妹~` 提交于 2019-12-05 01:25:43

问题


I'm loading the search results via jQuery ajax in a div container. I would like the results to be shown to the user after a 2 second delay or after the user has entered at least 3 letters/characters in the textbox to search. How would I do this?

jQuery code:

$(".bsearch").keydown(function() {
  //create post data
  var postData = { 
    "search" : $(this).val()
  };

  //make the call
  $.ajax({
    type: "POST",
    url: "quotes_in.php",
    data: postData, 
    success: function(response){
      $("#left").html(response);                    
      $("div#smore").hide();
    }
  });

回答1:


Use this function:

setTimeout(function() {
    $('#left').html(response);
}, 2000);



回答2:


use this function

function ajax_delay(str){
 setTimeout("str",2000);
}

results will be

 $.ajax({
    type: "POST",
    url: "quotes_in.php",
    data: postData, 
    success: function(response){

        ajax_delay($('#left').html(response));
$("div#smore").hide();
    }
  });

hope this helps you



来源:https://stackoverflow.com/questions/3497700/delay-load-of-data-for-2-seconds-in-jquery-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!