I want to make a loading... text using interval with jquery get method. Example as same as on this link:stackoverflow. I am able to make the text appear but how do I want to implement it on my get function.
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>
From my code,
- the loading text appear after clicked.
- the loading text won't fade out when the get function is done.
- Am I doing it the right way?
Update Code:
I'm able to get the output desire but I'm stuck on finding a way to clear the interval. Below are the updated code that I add,
$.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);
}
});
I want the dot(.) to repeat itself and stop until it has successful load the page from get method.
loadResult()which fades the loading... text out is called before the interval of 500ms which fades the text in. So fadeOut is called before fadeIn. Why are you setting the interval anyways? It will register a new interval everytime you click the button. Is that really what you want?