Only one span works

This code provides a pinpoint effect, like the download panel ("Download., Download .., Download ..."), but the problem is that only one space identifier works, the second does not, I don’t know why ... please help me 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>
+3
source share
4 answers

Just post the 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 affordable options

  • symbolcharacter to add to each iteration (default - .)
  • length maximum number of characters to display before it starts (default is 3)
  • delay time required to add each additional character (in milliseconds) (default 200)
  • duration ( ) ( 0, )

jsfiddle

$('some-selector').progress({
  symbol: '*',
  length: 10,
  delay: 100,
  duration: 5000 
});

, - .

var progressElements = $('some-selector').progress({/*options*/}); // set the progress
setTimeout(function(){
   progressElements.progress('clear');
 }, 1000); // 1000ms = 1 second

, duration.

, , .

http://jsfiddle.net/gaby/gh5CD/3/ ( 2 )

+1

, .

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/

+6

1: , / . .

2: Vision

Just out of interest ... I thought, what's the point of creating multiple timers and storing them in an array? If you wait a while, the individual timers will de-synchronize. How about this: jsfiddle.net/rMpK9/5

Improved code: (From Vision DEMO and use 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

Final demo

+3
source

Here is 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>

Fiddle

+1
source

All Articles