locked
Javascript timer doesn't work RRS feed

  • Question

  • User521171331 posted

    Hi, I have tried several ways to add timer of 2 seconds delay for each card dealt for my blackjack game, but it doesn't work still.

    I have tried the Promise and setTimeout but with no success. Here is my program - http://js.findingsteve.net/BlackJack.cshtml

    Monday, May 6, 2019 6:16 AM

All replies

  • User-1038772411 posted

    A Few Things To Notes :

    • <!DOCTYPE> instead of <DOCTYPE!>
    • id attributes should be unique, you have two declared
    • <center> is deprecated. Find a solution using CSS.
    • Rather than adding an onload attribute to your <body> I would suggest adding an DOMContentLoaded event listener to your document.

    <link rel="stylesheet" type="text/css" href="style_q1.css">
    
    <script type="text/javascript">
    
        function startTimer() {
            min = parseInt(timer / 60);
            sec = parseInt(timer % 60);
    
            if (timer < 1) {
                window.location = "index.html";
            }
    
            document.getElementById("time").innerHTML = "<b> Time Left: </b>" + min.toString() + ":" + sec.toString();
            timer--;
            setTimeout(function () {
                startTimer();
            }, 1000);
        }
    
        var timer = 60,
            min = 0,
            sec = 0;
    
        document.addEventListener('DOMContentLoaded', function () {
            startTimer();
        });
    </script>

    Reference Link

    Monday, May 6, 2019 8:33 AM
  • User839733648 posted

    Hi ngaisteve1,

    According to your description and program, I'm sorry that I could not reproduce your issue successfully.

    I've tried your program, I'm a little confused about your game's rule, could you explain the buttons' meaning?

    About your issue, if possible, please provide more information about your requirement like detailed code so that ut will be eaiser to help with you.

    Best Regards,

    Jenifer

    Tuesday, May 7, 2019 6:14 AM
  • User-2054057000 posted

    Hi, I have tried several ways to add timer of 2 seconds delay for each card dealt for my blackjack game, but it doesn't work still.

    See the below code which will call the function every 2 seconds time.

    var myVar = setInterval(myTimer, 1000);
    function myTimer() {
        console.log("I am called");
    }

    Reference which you should check - jQuery Timer tutorial 

    Wednesday, May 8, 2019 2:29 PM