When I press the start button during JS practice, I wrote to play rock-paper-scissors in 3, 2, and 1, but I get an error.

Asked 1 years ago, Updated 1 years ago, 21 views

When I pressed the start button during practice, I wrote to play rock-paper-scissors on 3, 2, 1, but I got an error. Uncaught SyntaxError: missing) after argument list came out, but it didn't work.I don't know if I'm doing something fundamentally strange, but please let me know.

<script>
             "use strict";

             vargyanken=["✊", "✌", "✋"];
             vari = 3;
             document.getElementById("start").getElementById("monitor").addEventListener("click", function(){ function show(){
                       monitor.innerHTML=(i--);
                       vartime = setTimeout(function(){
                         show();
                       }, 1000);
                       if(i<0){
                          clearTimeout(time);
                        //  start.addEventListener("click", function(){
                          var result=Math.floor(Math.random()*3);
                          monitor.innerHTML = gyanken [result];
                       }}}
                       show();)
</script>

javascript

2022-09-30 18:07

1 Answers

If you apply indentation and plastic surgery, it will be easier to see and you may notice a mistake.

<script>
    "use strict";

    vargyanken=["✊", "✌", "✋"];
    vari = 3;
    document.getElementById("start").getElementById("monitor").addEventListener(
        "click", 
        function(){
            function show() {
                monitor.innerHTML=(i--);
                var time = setTimeout(
                                        function() {show();}, 
                                        1000
                                    );
                if(i<0){
                    clearTimeout(time);
                    //  start.addEventListener("click", function(){
                    var result=Math.floor(Math.random()*3);
                    monitor.innerHTML = gyanken [result];
                }
            }
        }
        show();
    );
</script>

Did you notice that the argument for addEventListener is strange?
Uncaught SyntaxError—This basic error is displayed during a syntax error.
missing)after argument list:) I'm getting in trouble for not closing after the argument list

I have some concerns, but for the time being
in the unknown function that we are feeding on the event. Wouldn't function declaration and execution work?

 document.getElementById("start").getElementById("monitor").addEventListener(
    "click", 
    function(){
        function show() {
            monitor.innerHTML=(i--);
            var time = setTimeout(
                                    function() {show();}, 
                                    1000
                                );
            if(i<0){
                clearTimeout(time);
                //  start.addEventListener("click", function(){
                var result=Math.floor(Math.random()*3);
                monitor.innerHTML = gyanken [result];
            }
        }
        show();
    }
);

Added working code.Please refer to it.

// Global Variables
vargyanken=["✊", "✌", "✋"];
varsec = 3;
var time = null;

// Bind click event (can be included in initialization functions such as online)
document.getElementById("monitor").addEventListener(
    "click", 
    function(){
        // Click-to-initialize
        sec = 3;
        if(null!=time) {clearTimeout(time);}
        time = null;
        // Start displaying
        show(); 
    }
);

/**
 * display processing function
 */
function show() {
    // I will waste it at the end of the re-processing, so I will separate it by IF.
    if(0<=--sec){
        // Refresh Display
        monitor.innerHTML = sec+1;
        // Reboot after 1 second
        time = setTimeout(
                              function() {show();}, 
                              1000
                          );
    } else {
        // Discard timers if they exist
        if(null!=time) {clearTimeout(time);}
        // Initialize timer variable
        time = null;

        // Random number generation
        var result=Math.floor(Math.random()*3);
        // Display Results
        monitor.innerHTML = gyanken [result];
    }
}
#monitor{
  background-color:#d0f0f0;
  width:180px;
  height : 120px;
  cursor —pointer;
}
<divid="monitor"></div>


2022-09-30 18:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.