嗨,我在 ASP.net MVC3 中遇到了这个 Javascript 运行时错误。下面的代码片段工作得很好,但是当我尝试将 displayTime() 放入另一个 .js 文件时,问题就出现了。我尝试将整个 javascript 函数放在一个单独的 js 文件中,然后添加另一个像这样的导入:
<script src="@Url.Content("~/Scripts/customFunction.js")" type="text/javascript"></script>
在我导入脚本之后...所有这些代码都放在我的 _Layout.cshtml 文件中。但不幸的是,在说 Object Expected.... 时发生了 MS JScript 运行时错误<body onload="displayTime()">
:( 希望有人能提供帮助
<body onload="displayTime()">
<!-- scripts -->
<script src="@Url.Content("~/Scripts/jquery-1.7.2.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.20.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script type="text/javascript">
function displayTime()
{
}
</script>
</body>
下面是 JS 函数的完整代码:
var serverTime = @DateTime.Now.TimeOfDay.TotalSeconds;
var serverOffset = serverTime - getClientTime();
function getClientTime()
{
var time = new Date();
return (time.getHours() * 60 * 60) +
(time.getMinutes() * 60) + (time.getSeconds());
}
function displayTime()
{
var serverTime = getClientTime() + serverOffset;
var hours = Math.floor(serverTime / 60 / 60).toString();
var minutes = Math.floor(serverTime / 60 % (hours * 60)).toString();
var seconds = Math.floor(serverTime % 60).toString();
hours = hours.length == 1 ? '0'+ hours : hours;
minutes = minutes.length == 1 ? '0'+ minutes : minutes;
seconds = seconds.length == 1 ? '0'+ seconds : seconds;
document.getElementById("clock").innerHTML = hours + ":" +
minutes + ":" + seconds; // <-- updates the "clock" div.
setTimeout(displayTime, 1000); // <-- calls this function again in 1 second.
}