奇怪'预期;'我找不到的错误

时间:2016-09-16 10:59:04

标签: javascript html css syntax

我之前在这里发布了关于一个完全不同的问题,但是现在我的时钟(当单步执行时,似乎工作完全正常)返回';预期的'错误,大约每500毫秒,我会根据我的变量“t”猜测。然后在8-12次迭代后停止。说错误在第3行,即html文件的第1列。

var_dump($url);
function startTime() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();
    m = checkTime(m);
    s = checkTime(s);
    var t = setInterval(startTime(), 500);
	return h + ":" + m + ":" + s;
    
}
function checkTime(i) {
    if (i < 10) {i = "0" + i};
    return i;
}
.headtitle {
	color:red;
	font-family: "Courier New", Courier, monospace;
	display: flex;
	flex-wrap: wrap;
	justify-content: center;
	border: 5px solid #00f;
	padding: 10px;
}
.subtitle{
	display: flex;
	flex-wrap: wrap;
	justify-content: center;
	font-size: 1.25rem
}
a.firstlink{
	word-spacing: 30px;
}
a:visited{
	color:lightblue
}
body{
	background-image: url("http://i.kinja-img.com/gawker-media/image/upload/a942qcqwrcmveiq37zli.png");
	background-repeat:repeat;
	color: white;
}
.list{
	display: inline;
}
.list li{
	display: inline;
	padding-left: 1.5rem;
}
.button{
	float:right;
	border: 1px solid #f00;
	padding: 5px
}
.button:active{
	position: relative;
	top: 2px;
	left: 2px;
}
.JavascriptButton{
	float:right;
}
.clickText{
	cursor:pointer;
}

3 个答案:

答案 0 :(得分:1)

您只需更改一个startTime showTimesetInterval

同样,您可以将函数移交给setInteval而不调用它。

var t = setInterval(startTime, 500);
//                          ^^^ no parenthesis

function startTime() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();
    m = checkTime(m);
    s = checkTime(s);       
    document.getElementById("currentTime").innerHTML = "The time is now " + h + ":" + m + ":" + s; // add this here        
}

function checkTime(i) {
    if (i < 10) {i = "0" + i};
    return i;
}
.headtitle {
	color:red;
	font-family: "Courier New", Courier, monospace;
	display: flex;
	flex-wrap: wrap;
	justify-content: center;
	border: 5px solid #00f;
	padding: 10px;
}
.subtitle{
	display: flex;
	flex-wrap: wrap;
	justify-content: center;
	font-size: 1.25rem
}
a.firstlink{
	word-spacing: 30px;
}
a:visited{
	color:lightblue
}
body{
	background-image: url("http://i.kinja-img.com/gawker-media/image/upload/a942qcqwrcmveiq37zli.png");
	background-repeat:repeat;
	color: white;
}
.list{
	display: inline;
}
.list li{
	display: inline;
	padding-left: 1.5rem;
}
.button{
	float:right;
	border: 1px solid #f00;
	padding: 5px
}
.button:active{
	position: relative;
	top: 2px;
	left: 2px;
}
.JavascriptButton{
	float:right;
}
.clickText{
	cursor:pointer;
}
<!DOCTYPE html>
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="Website task.css">
		<script type="text/javascript" src="new 1.js"></script>
		<script type="text/javascript">
			function openFunction()
			{
				window.open('Website Task.html', '_blank');
			}
			function showTime()
			{
				var t = setInterval(startTime, 500);
				startTime();
			}
		</script>
		<title>Javascript page</title>
	</head>
	<body>
		<div class="header">
			<h1 class="headtitle">Javascript demo page</h1>
			<p class="subtitle">This page is for demonstrating functions in Javascript in particular, as well as jQuery.</p>
			<p id="currentTime"></p>
			<button id="timeButton" onclick="showTime()" onclick="removeElement('header','timeButton');">Click here to show the time</button>
			<p></p>
			<a class="clickText" onclick="openFunction()">Click this text to return to the previous page in a new window</a>
			<button class="JavascriptButton" onclick="window.location='Website Task.html';">Previous page</button>
		</div>
	</body>
</html>

答案 1 :(得分:0)

你有盈余;在你的if语句之后。成功:

storyboard

您遇到的错误也是超出了最大调用堆栈大小。这是因为你的函数setTime()正在调用它自己

答案 2 :(得分:0)

问题是当你setInterval(startTime(), 500);时,它实际上是setInterval("04:43:00", 500);setInterval期望第一个参数为函数。

示例

&#13;
&#13;
function startTime(){
  return new Date().toLocaleTimeString();
}

setTimeout(startTime(), 500)
&#13;
&#13;
&#13;