-8

I used this code but it's not repeat

it increase the value but don't get to the initial value 0
please help

   i=0;
        function loading()
        {
            text=["Loading.","Loading..","Loading...","Loading...."];
            window.setInterval(wr(),500)
        }
        function wr()
        {
            if(i<4)
            {
                wr='document.getElementById("text").innerHTML=text[i]';
                alert(i);
            }
            else
                i=0;    
            return wr;
        }
3
  • 7
    Oh wow. That's a confusing way to pass code to setInterval. Apart from that "mess", you are never increasing i. Aug 4, 2013 at 22:40
  • 5
    How can so many things go wrong in so little code?
    – elclanrs
    Aug 4, 2013 at 22:45
  • Also, wr() is a function that upon running once, overrides itself with a string. Aug 4, 2013 at 22:48

2 Answers 2

2

Your code is way to complicated. Define a simple function which you pass to setInterval and make sure you increase the counter variable:

var i = 0; // initialize counter
var textElement =  document.getElementById("text");
setInterval(function() {
    i = i % 4; // make sure `i` is at max 3 and reset to 0
    var text = 'Loading';
    for (var j = i; j--; ) {
        text += '.'; // add the correct number of periods
    }
    textElement.innerHTML = text; // set text
    i++; // increase counter
}, 500);

DEMO

1

Here's a slight modification of a similar answer I gave a while back.

var i = 0;
setInterval(function() {
    i = ++i % 4;
    document.getElementById('text').innerHTML = "Loading"+Array(i+1).join(".");
}, 500);
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.