Search code examples
javascriptrestartsweetalert2

How to use sweetalert2 to Restart (JavaScript)


Old code: restart("👎 Game Over. Try again!");

    playerLives = 10;
    playerLivesCount.textContent = playerLives;
    setTimeout(() => window.alert(text), 100);
};

This works great in a popup window. I need a pretty popup so i decided to use sweetalert2.

New code: Swal.fire("👎 Game Over. Try again!");

But now "OK" button does not run the function Restart.

How to use restart with Swal.fire ? enter image description here


Solution

  • Assuming that restart is a function that restarts the game, you could write the alert as follows:

    Swal.fire("👎 Game Over. Try again!").then(() => {
      restart();
    });
    

    You would also want to remove the logic from the restart function that previously fired an alert.