Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 72388

How can I write my callback function in JavaScript? [closed]

$
0
0

When regame() is called I call another function inside it i.e. anim1() by passing reset function as a callback function. However reset gets executed along with anim1() itself. I want my custom animation to end and then call the reset().

function regame() {
  $(".title").text("GAME OVER");
  anim1(reset);
  pc = [];
  user = [];
}

function reset() {
  if (window.matchMedia('(max-width: 768px)').matches)
    $(".title").text("Touch anywhere on the screen to start   the game");
  else
    $(".title").text("Press any key to start the game");
}

function anim1(callback) {
  var blre = ["black", "red"];
  var i = 0;
  var count = 0;
  var anime = setInterval(function() {
    $("body").css("background-color", blre[0 + i]);
    $(".title").css("color", blre[1 - i]);
    ++count;
    i = count % 2;
    if (count >= 7)
      clearInterval(anime);
  }, 150);
  callback();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Viewing all articles
Browse latest Browse all 72388

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>