Run function with timer in Javascript / Jquery

1

I need help creating a function in javascript / jquery to do the following:

  • When the user accesses my site, it starts timer.
  • If the person tries to close the browser window / tab before a certain time, eg. 5 sec, then it performs a function.
  • Now if the person spends more than 5 sec on the site the function will not be executed.
asked by anonymous 02.07.2015 / 16:22

1 answer

1

See if this is:)

var contador = 5;

setTimeout(temporizador,1000);

function temporizador() {
  if(contador > 0){
    setTimeout(temporizador,1000);
  } else {
    window.onbeforeunload = null;
  }
  document.getElementById('tempo').innerHTML = contador;
  contador--;
}

window.onbeforeunload = function() {
  // chama função
  return "Chamar uma função antes do tempo";
}
<p> Tempo restante: 
    <span id="tempo" />
</p>
    
02.07.2015 / 19:26