在ajax调用中设置延迟
我试图在加载程序图标和成功之间添加一个小延迟(2秒),数据为html。
我试图使用的是setTimeout并输入一个延迟号。 这不起作用,所以我希望你能告诉我正确的方法是什么。
我的ajax代码:
$(function () { var delay = 2000; var res = { loader: $("", { class: "loader" }) }; $('#search').on('click', function () { $.ajax({ type: 'GET', url: "@Url.Action("Find", "Hotel")", datatype: "html", beforeSend: function () { $("#group-panel-ajax").append(res.loader); setTimeout(delay); }, success: function (data) { $("#group-panel-ajax").find(res.loader).remove(); $('#group-panel-ajax').html($(data).find("#group-panel-ajax")); } }); return false; }); });
现在它运行得非常快。 希望有人能提供帮助。
应该在success
function
内使用setTimeout
。
$(function() { var delay = 2000; var res = { loader: $("", { class: "loader" }) }; $('#search').on('click', function() { $.ajax({ type: 'GET', url: "@Url.Action("Find", "Hotel")", datatype: "html", beforeSend: function() { $("#group-panel-ajax").append(res.loader); }, success: function(data) { setTimeout(function() { delaySuccess(data); }, delay); } }); return false; }); }); function delaySuccess(data) { $("#group-panel-ajax").find(res.loader).remove(); $('#group-panel-ajax').html($(data).find("#group-panel-ajax")); }
这是我想要做同样的事情:
function doStuff() { //do some things setTimeout(continueExecution, 10000) //wait ten seconds before continuing } function continueExecution() { //finish doing things after the pause }
希望它能帮助你
像这样使用setTimeout()
:
$(function() { var delay = 2000; var res = { loader: $("", { class: "loader" }) }; $('#search').on('click', function() { $.ajax({ type: 'GET', url: "@Url.Action("Find", "Hotel")", datatype: "html", beforeSend: function() { $("#group-panel-ajax").append(res.loader); }, success: function(data) { setTimeout(function() { delaySuccess(data); }, delay); } }); return false; }); }); function delaySuccess(data) { $("#group-panel-ajax").find(res.loader).remove(); $('#group-panel-ajax').html($(data).find("#group-panel-ajax")); }