[英]Set a delay in ajax call
我試圖在加載器圖標和數據為 html 的成功之間添加一個小的延遲(2 秒)。
我嘗試使用的是 setTimeout 並輸入延遲數。 這是行不通的,所以我希望你能告訴我正確的方法是什么。
我的ajax代碼:
<script type="text/javascript">
$(function () {
var delay = 2000;
var res = {
loader: $("<div />", { 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;
});
});
</script>
現在它運行得非常快。 希望有人能幫忙。
應該在success
function
內使用setTimeout
。
$(function() { var delay = 2000; var res = { loader: $("<div />", { 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")); }
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
這是我想要做同樣的事情:
function doStuff()
{
//do some things
setTimeout(continueExecution, 10000) //wait ten seconds before continuing
}
function continueExecution()
{
//finish doing things after the pause
}
希望它能幫助你
像這樣使用setTimeout()
:
<script type="text/javascript">
$(function () {
var delay = 2000;
var res = {
loader: $("<div />", { 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(){
$("#group-panel-ajax").find(res.loader).remove();
$('#group-panel-ajax').html($(data).find("#group-panel-ajax"));
}, delay);
}
});
return false;
});
});
</script>
$(function() { var delay = 2000; var res = { loader: $("<div />", { 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")); }
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.