2016-07-20 63 views
3

我是一名教師,並剛剛開始學習代碼爲我的學生進行在線測驗。我對JavaScript和PHP等編程還很陌生,我試着在線查找源代碼以幫助創建測驗。我有2個問題: 1)。我設置了一個定時器測驗,但每次當時間到了,它只是不斷計數,我應該放在顯示和計算得分爲javascript測驗

   if (parseInt(min) == 0) { 
        clearTimeout(tim); 
        location.href = ""; 

部分,我的學生重定向到結果頁面或其他頁面?

(2)我的測驗主要是填空題,我不知道如何存儲每個問題的要點,然後在測驗結束時向學生顯示總分。非常感謝!。 這裏是我的代碼:

<html> 
<head> 

<script language ="javascript" > 
    var tim; 

    var min = 0; 
    var sec = 30; 
    var f = new Date(); 
    function f1() { 
     f2(); 
     document.getElementById("starttime").innerHTML = "Your started your quiz at " + f.getHours() + ":" + f.getMinutes(); 


    } 
    function f2() { 
     if (parseInt(sec) > 0) { 
      sec = parseInt(sec) - 1; 
      document.getElementById("showtime").innerHTML = "Your Left Time is :"+min+" Minutes ," + sec+" Seconds"; 
      tim = setTimeout("f2()", 1000); 
     } 
     else { 
      if (parseInt(sec) == 0) { 
       min = parseInt(min) - 1; 
       if (parseInt(min) == 0) { 
        clearTimeout(tim); 
        location.href = "www.rawlanguages.com"; 
       } 
       else { 
        sec = 60; 
        document.getElementById("showtime").innerHTML = "Your Left Time is :" + min + " Minutes ," + sec + " Seconds"; 
        tim = setTimeout("f2()", 1000); 
       } 
      } 

     } 
    } 

</script> 

    <title>Quiz</title> 
    <h1>P.1 Grammar Quiz</h1> 
    <body> 

    <div id="ques0" class="ques"> 
    <h2>Question</h2> 
    <p>She 
    <input type="text" name="answer0"/> a girl.</p> 
    </div> 

    <div id="ques1" class="ques"> 
    <h2>Question</h2> 
    <p>"is", "am" and "are" are</p> 
    <ul> 
    <li> 
     <input type="radio" name="answer1" value="Present tense" /> 
     <label>Present tense</label> 
    </li> 
    <li> 
     <input type="radio" name="answer1" value="Past tense" /> 
     <label>Past tense</label> 
    </li> 
    <li> 
     <input type="radio" name="answer1" value="Future tense" /> 
     <label>Future tense</label> 
    </li> 
    </ul> 
</div> 

<div id="ques2" class="ques"> 
<h2>Question</h2> 
<p>He 
<input type="text" name="answer2"/> a policeman. 
</p> 
</div> 


<a href="javascript:checkAnswer()">Check answer!</a> 

<script src="JQ.js"></script> 
<script src="function.js"></script> 

<body onload="f1()" > 
<form id="form1" runat="server"> 
<div> 
    <table width="100%" align="center"> 
    <tr> 
     <td colspan="2"> 

     </td> 
    </tr> 
    <tr> 
     <td> 
     <div id="starttime"></div> 

     <div id="endtime"></div> 

     <div id="showtime"></div> 
     </td> 
    </tr> 
    <tr> 
     <td> 





     </td> 

    </tr> 
    </table> 




</div> 
</form> 

</body> 
</head> 
</html> 
+0

除非你解析一個字符串,否則你不需要'parseInt()'。您可以將它從現在使用的任何地方移除。 'if(parseInt(min)== 0)'永遠不會是真的,因爲'min'從0開始,並在前一行遞減1。 – JJJ

+0

當你說「存儲點」時,你的意思是暫時的(足夠長)以及永久性的(足夠長以便在學期結束時看到)? –

+0

這是給學生練習嗎?如果您只使用JS,那麼可以在右鍵單擊源代碼中找到答案。使用像PHP這樣的服務器端語言,您不能訪問答案。 JavaScript是客戶端語言,因此源可以被用戶讀取(即右鍵點擊>檢查)。 – zer00ne

回答

1

你的代碼是初學者不夠好,但它需要一些改進。

<script type="text/javascript" >//language ="javascript" is obsolete 
    //var tim; //no need at all 

    //var min = 0; //no need at all 
    //var sec = 30; //there is better way 
    //var f = new Date(); //no need to be global 
    function f1(sec) {//define (declare) sec as parameter 
     f2(); //call the function 
     var f = new Date(); 
     document.getElementById("starttime").innerHTML = "Your started your quiz at " + f.getHours() + ":" + f.getMinutes(); 
     var showtime = document.getElementById("showtime"); //used many times 
     //Here we put (closure) f2 
     function f2() { 
      //f2 knows sec from parent scope 
      if (sec <= 0) {//parseInt(sec) no need. sec is int 
      showtime.innerHTML = 'Time is over'; 
      //ShowAnswers(); //show on the same page or post to .php 
      return; 
      } 
      sec--;// = parseInt(sec) - 1; 
      showtime.innerHTML = "Your Left Time is :" + Math.floor(sec/60) +" Minutes ," + (sec % 60) +" Seconds"; 
      setTimeout(f2, 1000);//"f2()" is correct but this way is better 
     /* no need in remaining code 
     } 
     else { 
      if (parseInt(sec) == 0) { 
       min = parseInt(min) - 1; 
       if (parseInt(min) == 0) { 
        clearTimeout(tim); 
        location.href = "www.rawlanguages.com"; 
       } 
       else { 
        sec = 60; 
        document.getElementById("showtime").innerHTML = "Your Left Time is :" + min + " Minutes ," + sec + " Seconds"; 
        tim = setTimeout("f2()", 1000); 
       } 
      } 

     } 
     */ 
    }//f2 
}//f1 
</script> 

<body onload="f1(90)"><!--Here we send seconds to the function --> 

另外請注意,所有的測驗從<h1>P.1...開始必須是內body容器。