Repeat action every few seconds #PHP

0

And creating a project and I needed that when the user clicked on the "START POINTS" button the Code of PHP was activated, every 10 seconds he would gain 1 point in database and this would be repeated until the user click the "STOP POINTS" button.

I have already done the points part, but how do I do the "timer"?

It would be more or less like this:

Every 10 seconds it runs:

$sql_soma = mysql_query("UPDATE users SET cpoints = cpoints+1 WHERE users.user_email = '$e_mail'");
    
asked by anonymous 26.08.2018 / 17:03

1 answer

2

Then, you could do using only Javascript , and then get the value with PHP and save it to the database. So you would not have to run querys every few minutes to update the points in the database.

Here is a small example I made here using setInterval of Javascript .

let iniciar = document.getElementById('iniciar');
let finalizar = document.getElementById('finalizar');
let pontuacao = 0;
let contador = 0;
let interval = null;

iniciar.addEventListener('click', function () {
  console.log('iniciou, espere os 10 segundos.');
  interval = setInterval(contaPontos, 10000);
});

finalizar.addEventListener('click', function () {
  console.log('parou');
  clearInterval(interval);
});

function contaPontos() {
	contador++;
  document.getElementById("pontos").innerHTML = contador;
}
<button type="button" id="iniciar">Iniciar Jogo</button>
<button type="button" id="finalizar">Finalizar jogo</button>
<p id="pontos"></p>
    
26.08.2018 / 18:08