我想做一个loading ...使用interval
和jquery
get
方法。示例与此link:stackoverflow上的相同。我可以使文本显示,但我如何在我的get
函数上实现它。点击后如何实现加载文本的时间间隔?
jQuery(document).ready(function($) {
$(document).on("click", ".rekod", function() {
i = 0;
setInterval(function() {
i = ++i % 4;
$("#loading").html("loading" + Array(i + 1).join(".")).fadeIn();
}, 500);
var id = $(this).attr("id").split("-"),
pageNo = parseInt(id[1]);
loadResult(pageNo);
});
function loadResult(param) {
$.get("result-updated.php", {
startrow: param
}, function(data) {
$('#showAll').html(data);
}).done(function() {
$('#loading').fadeOut("slow");
});
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="showAll"></div>
<span id="loading"></span>
从我的代码,
- 加载文本显示。
- 当get函数完成时,加载文本不会淡出。
- 我正确的做法吗?
更新代码:
我能够得到的输出欲望,但我卡在寻找一种方法来明确的时间间隔。下面是更新后的代码,我添加,
$.ajaxSetup({
beforeSend :
function(){
i = 0;
var loading = setInterval(function() {
i = ++i % 4;
$("#loading").html("loading"+Array(i+1).join("."));
//clearInterval(loading);
//It work if I insert here but the dot(.) will only appear once.
}, 500);
},
complete :
function(){
clearInterval(loading); //The function is not working here.
setTimeout(function(){
$('#loading').html("Start");
},2000);
}
});
我想点(。)重演,停止,直到它有成功的负荷get方法的页面。
您可以在淡出添加到更迭的功能? (数据){ 函数(数据){('#showAll')。html(data); $('#loading')。fadeOut(“slow”); }) – Roelant
http://stackoverflow.com/questions/23906956/show-loading-icon-until-the-page-is-load – Brad
我认为'loadResult()'淡出加载...文本是在500ms间隔之前调用,淡入文本。所以fadeOut在调用之前被调用。 你为什么要设置间隔呢?每次单击按钮时,它将注册一个新的时间间隔。那真的是你想要的吗? – Schlangguru