How to pause and continue after an event?

0

To with this code here:

function aparecer(){
  const array1 = [1,2,3,4,5,6,8,9,10];
    for (let i=0; i < array1.length;i++){
      const lugar = document.getElementById('local');
     
     const ul = document.createElement('UL');
     const valor = document.createTextNode(array1[i])
      ul.appendChild(valor);
      
      lugar.appendChild(ul)
      break
    
      } 
    
}

function desaparecer(){
 document.getElementById('local').innerHTML= '';
  
}
<html>
  <button onClick='aparecer()'> Aparecer </button>
  
  <button onClick='desaparecer()'> Desaparecer </button>
  
  <div id='local'> </div>
  
</html>

And I want that with each click, only 3 numbers appear. Example: I clicked aparecer , then it appears: 1,2,3 clicked again, 4,5,6 clicked again, 7,8,9 and then click again to appear only 10.

Always creating a new UL, not replacing the current one.

How do I do this?

    
asked by anonymous 19.02.2018 / 19:30

1 answer

2

var current  = 0;
var array1 = [1,2,3,4,5,6,8,9,10];

function aparecer(){
	//verifica se o current é maior do que o array
	if ( (array1.length - 1) < current ) {
		alert("oi");
		//isso deveria parar a função
		return false;
	}
	for (var i= current; i <= (current + 2); i++){
		const lugar = document.getElementById('local');

		const ul    = document.createElement('UL');
		const valor = document.createTextNode(array1[i])
		ul.appendChild(valor);

		lugar.appendChild(ul);

	} 
	current = i;
}

function desaparecer(){
 current = 0;
 document.getElementById('local').innerHTML= '';
  
}
<html>
  <button onClick='aparecer()'> Aparecer </button>
  
  <button onClick='desaparecer()'> Desaparecer </button>
  
  <div id='local'> </div>
  
</html>
    
19.02.2018 / 19:46