Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
实现计时器Javascript的困难_Javascript_Html_Timer - Fatal编程技术网

实现计时器Javascript的困难

实现计时器Javascript的困难,javascript,html,timer,Javascript,Html,Timer,我一直在尝试为我正在开发的这个琐事游戏实现一个计时器,但我不明白它在我的情况下是如何工作的。我有以下代码: JS: var问题=[ { “问题”:“下一个Playstation平台将是:”, “allAnswers”:[“PS5”、“PS4K”、“PSX”、“PS8K”], “正确答案”:“PS4K” }, { “问题”:“树懒的学名Bradypus是希腊语的:”, “allAnswers”:[“缓慢移动”、“快速移动”、“缓慢行走”、“睡眠”], “正确答案”:“慢脚” }, { “问题”:“

我一直在尝试为我正在开发的这个琐事游戏实现一个计时器,但我不明白它在我的情况下是如何工作的。我有以下代码:

JS:

var问题=[
{
“问题”:“下一个Playstation平台将是:”,
“allAnswers”:[“PS5”、“PS4K”、“PSX”、“PS8K”],
“正确答案”:“PS4K”
},
{
“问题”:“树懒的学名Bradypus是希腊语的:”,
“allAnswers”:[“缓慢移动”、“快速移动”、“缓慢行走”、“睡眠”],
“正确答案”:“慢脚”
},
{
“问题”:“地球离太阳有多远:”,
“allAnswers”:[“9296万英里”、“8296万英里”、“10296万英里”、“7296万英里”],
“正确答案”:“9296万英里”
},
{
“问题:”《国家地理》第一期发行于:“,
“allAnswers”:[“1901”、“1874”、“1888”、“1926”],
“正确答案”:“1888”
},
{
“问题”:“金星上的温度有多高:”,
“allAnswers”:[“262摄氏度”、“162摄氏度”、“462摄氏度”、“862摄氏度”],
“正确答案”:“462摄氏度”
},
{
“问题”:“太阳是什么类型的恒星:”,
“allAnswers”:[“白矮星”、“黄矮星”、“太阳不是恒星”、“橙矮星”],
“正确答案”:“黄矮人”
},
{
“问题”:“大约有多少海洋已经被开发了”:,
“allAnswers”:[“12%,“23%,“38%,”5%,
“正确答案”:“5%”
},
{
“问题”:“阿波罗登月任务大约需要多长时间:”,
“allAnswers”:[“36小时”、“24小时”、“72小时”、“96小时”],
“正确答案”:“72小时”
},
{
“问题”:“脸上最平衡的勺子的世界记录是:”,
“allAnswers”:[“45匙”、“62匙”、“31匙”、“91匙],
“正确答案”:“31匙”
},
{
“问题”:“一分钟内单脚穿袜子最多的世界纪录是:”,
“allAnswers”:[“12只袜子”、“28只袜子”、“48只袜子”、“67只袜子”],
“正确答案”:“48只袜子”
}];
var content=document.getElementById(“内容”);
var questionHolder=document.getElementById(“问题”);
var choicesHolder=document.getElementById(“选项”);
var scoreHolder=document.getElementById(“分数”);
var timeHolder=document.getElementById(“时间”);
var submitButton=document.getElementById(“提交”);
var=0;
var得分=0;
无功定时器=20;
var=true;
//var myTimer=setInterval(askQ(),10000);
函数askQ()
{
var choices=问题[questionTracker].allAnswers;
var=”;
for(var i=0;i”;
} 
/*加载问题*/
questionHolder.textContent=“问题:”+(questionTracker+1)+“+”问题[questionTracker]。问题;
timeHolder.textContent=“时间:”+计时器+“还剩几秒。”;
/*加载答案*/
choicesHolder.innerHTML=tempChoices;
tim=设置超时(askQ(),1000);
/*设置,第一次迭代*/
如果(questionTracker==0)
{
scoreHolder.textContent=“分数:0正确回答了“+问题.长度+”总问题中的问题。”;
//timeHolder.textContent=“时间:”+计时器+“还剩几秒。”;
submitButton.textContent=“提交Q”;
}
}
函数validateAnswer()
{
/*检查是否提出了问题,确定是否继续下一个问题*/
如果(被问到)
{
submitButton.textContent=“下一个Q”;
wasQuestionAsked=假;
/*确定单击了哪个单选按钮*/
var用户选择,更正索引;
var radios=document.getElementsByName(“测验”+questionTracker);
对于(变量i=0;i“+”您的百分比是:“+数学.四舍五入(分数/问题.长度*100)+
“%”“+”
“+”重新启动“; } /*将重置游戏的功能*/ 函数重置() { location.reload(); } window.addEventListener(“加载”,askQ,false); submitButton.addEventListener(“单击”,validateAnswer,false);
还有一些HTML:

<div id="content">
                <h3 id="question" style="color:orange;">

                </h3>
                <div id="choices">

                </div>
                <br>
                <p><button id="submit" class="btn btn-warning"></button>
                </p>

                <p id="score">

                </p>
                <p id="time">

                </p>
              </div>


我所想要的就是当一个问题出现时,计数器出现,并从20开始倒数。我一直在检查W3中的setInterval()和setTimeout(),但似乎无法使它们正常工作。如果有人能看一下代码并帮助我,以及任何提示/帮助,将不胜感激。谢谢

下面是一个例子:

<!-- output timer - this will be updated every second. -->
<div id="output-timer" style="position: absolute; top: 0%; left: 0%;">
</div>

<script>
    // This is a recursive function, if intTimer is 0 it will return
    // without doing anything...if intTimer > 0 it will:
    //
    //    1. decrease the timer.
    //    2. update the output.
    //    3. call itself...
    //
    function updateTimer(intTimer) {
        // timer has expired.
        //
        if (intTimer == 0)
            return;

        setTimeout(function() {
            // Decrease timer...
            //
            intTimer--;

            // Update the output...
            //
            document.getElementById('output-timer').innerHTML = intTimer.toString();

            // Next.
            //
            updateTimer(intTimer);
        }, 1000);
    }

    // Initialise the timer ad begin timing!
    //
    function initTimer(intTimer) {
        document.getElementById("output-timer").innerHTML = intTimer.toString();

        updateTimer(intTimer);
    }

    initTimer(20);
</script>

//这是一个递归过程
<!-- output timer - this will be updated every second. -->
<div id="output-timer" style="position: absolute; top: 0%; left: 0%;">
</div>

<script>
    // This is a recursive function, if intTimer is 0 it will return
    // without doing anything...if intTimer > 0 it will:
    //
    //    1. decrease the timer.
    //    2. update the output.
    //    3. call itself...
    //
    function updateTimer(intTimer) {
        // timer has expired.
        //
        if (intTimer == 0)
            return;

        setTimeout(function() {
            // Decrease timer...
            //
            intTimer--;

            // Update the output...
            //
            document.getElementById('output-timer').innerHTML = intTimer.toString();

            // Next.
            //
            updateTimer(intTimer);
        }, 1000);
    }

    // Initialise the timer ad begin timing!
    //
    function initTimer(intTimer) {
        document.getElementById("output-timer").innerHTML = intTimer.toString();

        updateTimer(intTimer);
    }

    initTimer(20);
</script>