我有一个输入字段,我想每2秒左右刷新一次。但是,我似乎无法获得'setInterval',因为它没有更新我的ID。我不应该使用 要声明刷新的ID?任何帮助将非常感谢。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" content="HTML,CSS,XML,JavaScript">
<script src="Scripts/Global.js" type="text/javascript"></script>
<script src="Scripts/jquery.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="Styles/Design.css">
</head>
<body>
Time: <input type="time" id="theTime">
<script type="text/javascript"
src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
var myVar = setInterval(function(){ myTimer() }, 1000);
function myTimer() {
var d = new Date();
var t = d.toLocaleTimeString();
document.getElementById("theTime").innerHTML = t;
}
</script>
<script type="text/javascript">
$(document).ready( function() {
var now = new Date();
//now2 prevents the milliseconds from showing up in the input
var now2 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours()-8, now.getMinutes(), now.getSeconds());
$('#theTime')[0].valueAsDate = now2;
});
</script>
</body>
</html>
答案 0 :(得分:3)
您正在设置myTimer的innerHTML
,您应该真正设置valueAsDate
,就像在$(document).ready
功能中一样。
以下是myTimer
的外观:
function myTimer() {
var now = new Date();
var now2 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours()-8, now.getMinutes(), now.getSeconds());
$('#theTime')[0].valueAsDate = now2;
}
然后您可以在myTimer()
:
$(document).ready
$(document).ready( function() {
myTimer();
});
答案 1 :(得分:1)
您正在尝试设置输入字段的innerHTML。改为使用$('#theTime').val()
!