【问题标题】:referencing a variable with an undefined value引用具有未定义值的变量
【发布时间】:2020-03-18 00:05:58
【问题描述】:

我试图在计时器结束后提醒变量。该变量是一个具有未定义值的分数,因为它根据交互性而变化。但是,它显示为未定义。

<tr>
   <td id="scoreText">Score:</td>
   <td><input type="text" id="score" readonly></td>
  </tr>

html 是使用只读的文本输入类型创建的。但是,我可以提醒自己没有任何价值。分数值根据交互性而变化,如下面的代码所示。

  <script>
function hithead(id) {

                if(currentpos!=id) {
                    totalhits+=-1;
                    document.getElementById("score").value=totalhits;
                    document.getElementById(id).checked=false;
                }
                else {
                    totalhits+=1;
                    document.getElementById("score").value=totalhits;
                    launch();
                    document.getElementById(id).checked=false;

                    document.getElementById("theHiddenOne").value=totalhits;

                }

            }



   var showScore = document.getElementById("theHiddenOne").value;

            function showtime(remtime) {
                document.getElementById("timeleft").value=remtime;
                if(playing) {
                    if(remtime==0) {
                        alert('Game Over! \n Your Score is'  + showScore );
                        location.reload();
                    }
                    else {
                        temp=remtime-1;
                        setTimeout("showtime(temp)",1000);
                    }
                } 
            }
</script>

分数的值是通过 hithead 函数中的总命中数来确定的。

theHiddenOne 已创建,因此该值将显示在要引用的 html 中。我通过创建一个console.log 对此进行了测试,并给出了一个值。但是,我无法在我的 showScore 变量中引用这个值。

谢谢。

gamelength=30;
            timerID=null
            var playing=false;
            var numholes=6*10;
            var currentpos=-1;


            function clrholes() {
                for(var k=0;k<60;k++)
                document.getElementById(k).checked=false;
            }





            function play() {

                playing=true;
                clrholes();
                totalhits=0;
                document.getElementById("score").value=totalhits;
                launch();
                showtime(gamelength);


            }


            function launch() {
                var launched=false;

                while(!launched) {

                    mynum=random();

                    if(mynum!=currentpos) {
                        document.getElementById(mynum).checked=true;
                        currentpos=mynum;
                        launched=true;
                    }
                }
            }

【问题讨论】:

  • 我看不到hithead()showtime() 在任何地方被调用,如果它们没有被调用,那么showScore 的值将保持undefined
  • @AhmedHammad 我已经编辑了我的帖子以包含整个 javascript 代码。调用了 showtime(),但没有调用 hithead()。我该如何解决这个问题?

标签: javascript variables undefined alert


【解决方案1】:

使用此代码:

setTimeout("showtime(temp)",1000)// as a string

//should be like this
setTimeout(showtime.bind(temp),1000)

//or simpler
setTimeout(function(){
showtime(temp)
},1000)

【讨论】:

  • setTimeout("showtime(temp)",1000) 仍然可以正常工作,这不是问题
猜你喜欢
  • 1970-01-01
  • 2012-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-14
  • 2015-01-20
相关资源
最近更新 更多