引用具有不确定值的变量

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

<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;
                    }
                }
            }
jhcwengbin 回答:引用具有不确定值的变量

使用此代码:

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

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

//or simpler
setTimeout(function(){
showtime(temp)
},1000)
本文链接:https://www.f2er.com/3054116.html

大家都在问