Infinite counting of ordinal numbers and exposing in HTML document

Asked

Viewed 913 times

2

What I want to know is how I can make a rising counter in pure Javascript 0 , 1 , 2 , ...

I want something automatic, infinite without needing an arrow an integer or negative number

Counting can start from 0 or 1 onwards ...

Example

</script>

var min = 0    

var max = 9

for (var i = min; i < max; i++) {
  var seq = i/0;
  document.getElementById('num').innerHTML += i
}
<script>

<body>
   <span id='num'>&nbsp;</span>
</body>

1 answer

4


It’ll be something like this?

const eleResult = document.getElementById('contador');
var n = 0;
window.setInterval(function() {
  eleResult.innerHTML += n+ ' ';
  n++;
}, 100); // ajustas o tempo em milisegundos aqui
<div id="contador"></div>

To set a maximum (in this case 20) do:

const eleResult = document.getElementById('contador');
var n = 0;
const max = 20;
const conta = window.setInterval(function() {
  eleResult.innerHTML += n+ ' ';
  if(n == max) window.clearInterval(conta);
  n++;
}, 100); // ajustas o tempo em milisegundos aqui
<div id="contador"></div>

  • That’s right, but I wanted to let loose, successively 1 2 3 4 5 6 8 9 ... Would have how, reuse this your routine to leave us as?

  • Sure, I’ll edit @Diegohenrique

  • It was really good. We used the reserved word const, to achieve that objective.

  • @Diegohenrique that you use if you know that the value of the variable will not change throughout the program, but uses var . In this case it would still work with var

Browser other questions tagged

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