我正在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#剃刀变量调用此js函数。对一项进行基本操作,例如:
<body onload= "countdown('@item.EndDate')">
答案 0 :(得分:2)
尝试使用以下语法:
@foreach (var item in Model)
{
//code here
countdown(`${@item.EndDate}`)
}
答案 1 :(得分:1)
如果要使用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
答案 2 :(得分:0)
我想出了一种疯狂的方式来做我想做的事情,可能效率不高,可能对整个编码社区都是一种罪过,但这是:
@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。