为什么这段代码不起作用?

时间:2016-09-01 04:00:08

标签: javascript

我已经尝试了一切我能想到的工作。我知道你可以在Javascript中的函数中放置一个函数。我不知道还能做什么。有人能帮助我吗?

<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<title>Slideshow Webpage</title>
<script>
 var sound = new Audio ("Get low.mp3");


 function startTimer() {

 setTimeout(countnine, 1000);
 setTimeout(counteight, 2000);

 function ten() {

 document.getElementById("timerText").innerHTML="10";
 }

 function nine() {

 document.getElementById("timerText").innerHTML="9";
 }

 function eight() {

 document.getElementById("timerText").innerHTML="8";
 }

 function seven() {

 document.getElementById("timerText").innerHTML="7";
 }

 function six() {

 document.getElementById("timerText").innerHTML="6";
 }

 function five() {

 document.getElementById("timerText").innerHTML="5";
 }

 function four() {

 document.getElementById("timerText").innerHTML="4";
 }

 function three() {

 document.getElementById("timerText").innerHTML="3";
 }

 function two() {

 document.getElementById("timerText").innerHTML="2";
 }

 function one() {

 document.getElementById("timerText").innerHTML="1";
 }
 }
</script>
</head>
<body>
<h1>Testing this website for a webpage</h1>
<p>This is just a test. I am testing this website out to make a webpage.                  </p>

<div id="timer">
<p id="timerText"></p>
</div>

<p onclick="startTimer()">Start timer</p>
</body>
</html>

告诉我这件事我做错了什么?

2 个答案:

答案 0 :(得分:3)

你不需要再调用任何setTimeout,使用setTimeout。像这样的例子

function startTimer(){
var count = 10;
var time = setInterval(function(){
   
  if(count==0){
    clearInterval(time)
  }
   document.getElementById("timerText").innerHTML=count--
},1000)

}
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<title>Slideshow Webpage</title>
<script>
 var sound = new Audio ("Get low.mp3");


</script>
</head>
<body>
<h1>Testing this website for a webpage</h1>
<p>This is just a test. I am testing this website out to make a webpage.                  </p>

<div id="timer">
<p id="timerText"></p>
</div>

<p onclick="startTimer()">Start timer</p>
</body>
</html>

答案 1 :(得分:-2)

请使用此代码

&#13;
&#13;
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<title>Slideshow Webpage</title>
<script>
 var sound = new Audio ("Get low.mp3");


 function startTimer() {
 
 setTimeout(nine, 1000);
 setTimeout(eight, 2000);

 function ten() {

 document.getElementById("timerText").innerHTML="10";
 }

 function nine() {
 
 document.getElementById("timerText").innerHTML="9";
 }

 function eight() {

 document.getElementById("timerText").innerHTML="8";
 }

 function seven() {

 document.getElementById("timerText").innerHTML="7";
 }

 function six() {

 document.getElementById("timerText").innerHTML="6";
 }

 function five() {

 document.getElementById("timerText").innerHTML="5";
 }

 function four() {

 document.getElementById("timerText").innerHTML="4";
 }

 function three() {

 document.getElementById("timerText").innerHTML="3";
 }

 function two() {

 document.getElementById("timerText").innerHTML="2";
 }

 function one() {

 document.getElementById("timerText").innerHTML="1";
 }
 }
</script>
</head>
<body>
<h1>Testing this website for a webpage</h1>
<p>This is just a test. I am testing this website out to make a webpage.                  </p>

<div id="timer">
<p id="timerText"></p>
</div>

<p onclick="startTimer()">Start timer</p>
</body>
</html>
&#13;
&#13;
&#13;