我正在Asp.net MVC的一个拍卖网站上工作,我正在努力展示每个项目的拍卖会剩余多少时间的计时器。我使用我的模型将项目列表传递到我的cshtml页面,然后像这样迭代它们:
我的javascript函数启动计时器:
function countdown(time) {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = time - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="timeLeft"
document.getElementById("timeLeft").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
}, 1000);
然后是我的模型的迭代器,用项目的结束日期调用js函数
@foreach (var item in Model)
{
//code here
countdown(@item.EndDate)
<text id="timeLeft"></text>
}
我的脚本由<script src="~/Scripts/countdown.js" />
引用
我遇到的问题是如何用c#razor变量调用这个js函数。做一件基本的东西,如:
<body onload= "countdown('@item.EndDate')">
试试这个语法:
@foreach (var item in Model)
{
//code here
countdown(`${@item.EndDate}`)
}
如果你想在javascript中制作一个计时器,我会使用window.setInterval
函数,然后将第二个参数设置为时间间隔。
这个函数countdown
需要传递三个参数
function countdown(endYear,endMonth,endDay) {
window.setInterval(function () { StartCount(endYear, endMonth, endDay, 'andy timer'); }, 1000);
}
function StartCount(endYear,endMonth,endDay){
var now=new Date();
var endDate=new Date(endYear,endMonth-1,endDay);
var leftTime=endDate.getTime() - now.getTime();
var leftSecond=parseInt(leftTime/1000);
var day=Math.floor(leftSecond/(60*60*24));
var hour=Math.floor((leftSecond-day*24*60*60)/3600);
var minute=Math.floor((leftSecond - day * 24 * 60 * 60 - hour * 3600) / 60);
var second = Math.floor(leftSecond - day * 24 * 60 * 60 - hour * 3600 - minute * 60);
document.getElementById("timeLeft").innerHTML = day + "d " + hour + "h "
+ minute + "m " + second + "s ";
}
countdown(2018,10,5)
<div id='timeLeft'></div>
您可以在body标签上使用它
<body onload= "countdown(@Model.EndDate.Year,@Model.EndDate.Month,@Model.EndDate.Day)">
编辑
它在c#MVC在线工作:https://dotnetfiddle.net/ERcwAb
我想出了一个疯狂的方式来做我想要做的事情,可能效率不高,可能对整个编码社区来说是一种罪恶,但这里是:
@foreach (var item in Model)
{
<script>
function countdown(endYear, endMonth, endDay, endHours, endMin, num) {
window.setInterval(function () { StartCount(endYear, endMonth, endDay, endHours, endMin, num); }, 1000);
}
function StartCount(endYear, endMonth, endDay, endHours, endMin, num) {
var now = new Date();
var endDate = new Date(endYear, endMonth - 1, endDay, endHours, endMin);
var leftTime = endDate.getTime() - now.getTime();
var leftSecond = parseInt(leftTime / 1000);
var day = Math.floor(leftSecond / (60 * 60 * 24));
var hour = Math.floor((leftSecond - day * 24 * 60 * 60) / 3600);
var minute = Math.floor((leftSecond - day * 24 * 60 * 60 - hour * 3600) / 60);
var second = Math.floor(leftSecond - day * 24 * 60 * 60 - hour * 3600 - minute * 60);
var id = "timeLeft" + num;
document.getElementById(id).innerHTML = day + "d " + hour + "h "
+ minute + "m " + second + "s ";
}
countdown(@item.EndDate.Year, @item.EndDate.Month, @item.EndDate.Day,@item.EndDate.Hour, @item.EndDate.Minute, @num)
</script>
@{string countNum = "timeLeft" + num;}
<text id= @countNum></text>
@{num++;}
//code
}
但是它仍会异步加载项目将加载的所有内容,然后时钟都会在一秒钟之后出现,所有内容都会相应地移动。可能是由于我最初的问题是无法调用body onload。