Reputation: 455
This code provide a dot effect, like a loading bar(" Loading., Loading.., Loading...), but the problem , that only one single span id works, the second one wont, i dont know why... please help me out guys
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
function showProgressDots(numberOfDots) {
var progress = document.getElementById('progressDots');
switch(numberOfDots) {
case 1:
progress.innerHTML = '.';
timerHandle = setTimeout('showProgressDots(2)',200);
break;
case 2:
progress.innerHTML = '..';
timerHandle = setTimeout('showProgressDots(3)',200);
break;
case 3:
progress.innerHTML = '...';
timerHandle = setTimeout('showProgressDots(1)',200);
break;
}
}
window.setTimeout('showProgressDots(1)',100);
//-->
</SCRIPT>
Loading<span id="progressDots" style="position:absolute;"></span>
SecondLoading<span id="progressDots" style="position:absolute;"></span>
Upvotes: 3
Views: 197
Reputation: 196002
Just posting a little plugin i made for this (for the fun of it..).
(function($){
$.fn['progress'] = function(_options){
var options = {
symbol: '.',
delay: 200,
length: 3,
duration: 0
};
if (typeof _options === 'object'){
$.extend(options, _options);
} else if (_options === 'clear'){
return this.each(function(){
clear.apply(this);
});
}
function display(){
var self = $(this),
txt = self.text() + options.symbol;
if (txt.length > options.length){
txt = '';
}
self.text( txt );
}
function clear(){
var self = $(this),
timer = self.data('progressTimer');
clearInterval(timer);
self.text('');
self.removeData('progressTimer');
}
return this.each(function(){
var self = $(this),
that = this,
timer = null;
timer = setInterval(function(){
display.apply(that);
}, options.delay);
self.data('progressTimer', timer);
if (options.duration){
setTimeout(function(){
clear.apply(that);
}, options.duration);
}
});
}
})(jQuery);
You use it with
// to set it
$('some-selector').progress({/*options*/});
// to stop it
$('some-selector').progress('clear');
with available options
being
symbol
the character to add on each iterations (default is .
)length
the max number of symbols to display before it starts over (default is 3)delay
the time it takes for each extra symbol to be added (in milliseconds) (default is 200)duration
total duration (in milliseconds) before clearing the plugin (default is 0 which means no auto clearing)$('some-selector').progress({
symbol: '*',
length: 10,
delay: 100,
duration: 5000
});
update for comment
To clear it automatically after some specific time just add a timeout to your code..
so
var progressElements = $('some-selector').progress({/*options*/}); // set the progress
setTimeout(function(){
progressElements.progress('clear');
}, 1000); // 1000ms = 1 second
update for 2nd comment
changed the plugin code above to allow for a duration
parameter.
If specified, it declares the time after which the plugin will automatically clear..
Demo at http://jsfiddle.net/gaby/gh5CD/3/ (the second loader will clear after 2 seconds)
Upvotes: 1
Reputation: 79830
Edit 1: Wrote a customizable loader so you can register/un-register loaders.. See below,
Edit 2: As Vision pointed out
Just out of interest... I thought, what is the point to create multiple timers and store them in the array? If you wait for some time, the separate timers will desynchronize. How about this: jsfiddle.net/rMpK9/5
Improved Code: (From Vision's DEMO and use of getElementsByTagName
)
var timer = null,
dotLimit = 3,
elements = [];
function registerProgressDots(progress) {
for (var i = 0; i < progress.length; i++) {
elements.push(progress[i]);
}
timer = setInterval(function() {
for (var i = 0; i < elements.length; i++) {
with(elements[i]) {
innerHTML = innerHTML.length == dotLimit ? '' : innerHTML + '.';
}
}
}, 200);
}
function unRegisterProgressDots(index, clearDots) {
if (typeof index == 'undefined' || index == null) {
clearInterval(timer);
} else {
elements.splice(index, 1);
if (elements.length == 0) {
clearInterval(timer);
}
}
if (clearDots) {
var progress = document.getElementsByClassName('progressDots');
for (var i = 0; i < progress.length; i++) {
progress[i].innerHTML = '';
}
}
}
window.setTimeout(function() {
var spanTags = document.getElementsByTagName('span');
var progress = [];
for (var i = 0; i < spanTags.length; i++) {
if (spanTags[i].className.indexOf('progressDots') >= 0) {
progress.push(spanTags[i]);
}
}
registerProgressDots(progress);
}, 100);
window.setTimeout(function() {
unRegisterProgressDots(null, true);
}, 10000); //stop loading text after 10 seconds
Upvotes: 3
Reputation: 318222
Here's my suggestion:
Loading<span id="progressDots" style="position:absolute;"></span> //element before script
<script type="text/javascript">
var i='', dots=20;
showProgressDots();
function showProgressDots() {
var progress = document.getElementById('progressDots');
progress.innerHTML = i;
i+='.';
if (i.length<dots) setTimeout(showProgressDots, 200);
}
</script>
Upvotes: 1
Reputation: 145408
Here is the short solution if you need.
HTML:
Loading<span id="progressDots1"></span>
SecondLoading<span id="progressDots2"></span>
JavaScript:
function loading(id) {
var el = document.getElementById(id),
i = 0,
dots = "...";
setInterval(function() {
el.innerHTML = dots.substring(0, ++i);
if (i % 3 == 0) i = 0;
}, 500);
}
loading("progressDots1");
loading("progressDots2");
DEMO: http://jsfiddle.net/dzFL3/
Upvotes: 6