Trying to make a text ... loading animation
here\'s where I stand: http://jsfiddle.net/aGfct/
I can get the ... to be added at 500 ms intervals, but then I w
Try using setInterval also so like:
setInterval(function(){
for (i = 1; i <= 3; i++) {
setTimeout(function() {
$("#loading").append(".");
}, i * 500);
}
$("#loading").html('loading');
}, 2000);
http://jsfiddle.net/paska/aGfct/12/
var originalText = $("#loading").text(),
i = 0;
setInterval(function() {
$("#loading").append(".");
i++;
if(i == 4)
{
$("#loading").html(originalText);
i = 0;
}
}, 500);
I have a solution very similar like roXon, only in my case 2 features I have added.
Demo in JsFiddle
[HTML]
<!--Just have an element with id=loadingDots-->
<span style="font-size:42px">Sending<span id="loadingDots"></span></span>
[JS]
$(document).ready(function(){
/**Call the function in document.ready somewhere*/
showLoadingDots();
});
The function
var showLoadingDots = function() {
/**If element not found, do nothing*/
if (!$("#loadingDots").length>0) return false;
var showDots = setInterval(function(){
//Thanks to roXon, StackOverflow
var d = $("#loadingDots");
d.text().length >= 3 ? d.text('') : d.append('.');
},300);
}
Hope it helps somebody. Thanks roXon, for ideas.
Just add this line at the end of your loop:
i = (i === 3) ? 0 : i;
That's just shorthand for saying 'if i
is equal to 3, set it back to zero, else leave it as it is'. That should kick off your loop all over again until you set an exit condition.
EDIT: Hold on, I didn't actually look at how you appended the .
(sorry, can't get jsFiddle to run anything at the moment)! If you were to use the i
reset as above, you'd really need to set the number of .
characters equal to i
with every iteration.
EDIT 2: Looking again, you'd even need to take i
into a closure to get its value at the time the setTimeout
is declared, otherwise you'll get whatever value it is when setTimeout
is executed, which is unpredictable. Basically, don't use this solution - use Jeff's! ;)
i = 0;
setInterval(function() {
i = ++i % 4;
$("#loading").html("loading"+Array(i+1).join("."));
}, 500);
If you wish to change the string after 5 says, that's after 10 iterations. This can be accomplished like this.
i = 0;
text = "loading";
setInterval(function() {
$("#loading").html(text+Array((++i % 4)+1).join("."));
if (i===10) text = "start";
}, 500);