0

setInterval() does not work correctly when AJAX call is made. Here is the code

  xhttp.onreadystatechange = function() {
     
    document.getElementsByTagName('body')[0].innerHTML += this.responseText;
    clearInterval(downloadTimer);
    document.getElementsByTagName('body')[0].style="pointer-events:none;background: linear-gradient(#000000, #4a4949);height: 100%;margin: 0;background-repeat: no-repeat;background-attachment: fixed;";
    var sub = setInterval(alert('Hello'),3000);
  };

The problem is with var sub, it should show alert box after 3s, but it shows it right after AJAX call. It does not wait even a sec, I tried to put bigger value than 3000, but the result is the same. I've also tried to use setTimeout, but the result is the same. Any help is more than welcome! Kind Regards

P.S. Everything else in this block works just fine.

Rambo97
  • 11
  • 3
  • 1
    Wrap the alert inside a function like this: setInterval(()=>{alert('Hello')},3000) – Rinkesh Golwala Dec 05 '20 at 01:00
  • Personally, I would NEVER use `alert` inside an `setInterval` or inside `onreadystatechange` - I would never use an alert in the first place. Also, I'd never use the old onreadystatechange either - there's better ways to do everything you're trying to do – Bravo Dec 05 '20 at 01:03
  • @RinkeshGolwala Thx! That works – Rambo97 Dec 05 '20 at 01:04

0 Answers0