我正在开发一个基于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')">
当我放置我的剃须刀变量时,它会使我的函数变灰。我需要如何将我的变量传递到我的js函数中?
例如:(使用单个模型项)
{{link1:
![enter image description here](https://istack.dev59.com/J8F6o.webp)
![enter image description here](https://istack.dev59.com/mlTzF.webp)