Time interval in a while with javascript

Asked

Viewed 618 times

1

Hello, I have to do a function in javascript, however, I need to put a time interval in a while within that function (run the function while 1 time per second). Being that I have to 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);

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 takes as parameter a function and a time value in milliseconds (1000 ms = 1 second)

  • Thanks, it worked out!

  • Glad I could help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.