Time interval in a while with javascript

1

Hello, I have to do a function in javascript, however, I need to put a time interval in a while within that function (execute the while function 1 times per second). Being that I have make all this appear in a div in html

    function rotateText(id,dt)
    {
    var f = document.getElementById("id").value;
    var cont = 0;
    var fa, b, fb, c, y = [];
        // Esse while tem que ter o intervalo
        while(cont != 100){
        fa = f.length;
        fa = fa - 1;
        b  = f.charAt(fa);
        fb = f.slice(0,fa);
        c  = b.concat(fb);
        y = y +"<br>"+ c
        f = c;
       document.getElementById("Cosas").innerHTML = y;
        cont = cont + 1;
    }
  }

  window.addEventListener('DOMContentLoaded', function() {
rotateText(id, 1000);
    }, false);
    
asked by anonymous 17.05.2016 / 03:55

1 answer

3

Use the setInterval function.

function whileComIntervalo ()
{
    while (cont != 100)
    { /* Resto do código */ }
}

function rotateText(id,dt)
{ /* Declaração de variáveis */
    setInterval(whileComIntervalo, 1000);
}

setInterval receives a function and a time value in milliseconds (1000 ms = 1 second) as a parameter

    
17.05.2016 / 04:19