开发工具与关键技术:vs 计时器
作者:周乐献
撰写时间:2019/4/15
计时器这种东西在很多应用里面都有用到,根据我接触到的层面来说,我所知道的应用里面用到计时器的大多是游戏类的应用。比如说王者荣耀,刺激战场…等。我觉得运用的比较好的是那个游戏健康系统。它会根据你在实名登记时说注册的身份信息来判断你玩游戏的时间。当你未满18周岁时一个账号每天只玩限定的时间的游戏。并且当你当天游戏累计达到一定程度的时候会限制你游戏时间,让你休息几分钟才能在进行游戏。
<div class="dropdown-menu dropdown-menu-right user-dd animated flipInY" style="background:#ffd800; width:200px;height:200px;">
<span class="with-arrow">
<span class="bg-primary"></span>
</span>
<a class="dropdown-item" href="javascript:void(0)" style="display:inline-flex;">
<i class="fas fa-fw fa-clock"></i>
<h2 style="font-size:12px; color:#0094ff;">登录时间:</h2>@ViewBag.serverTime
</a>
<a class="dropdown-item" href="javascript:void(0)" style="display:inline-flex;">
<i class="fas fa-fw fa-stopwatch"></i>
<h2 style="font-size:12px; color:#b200ff;">登录时长:</h2>
<span id="LoginTime" ></span>
</a>
</div>
<script>
$(function () {
www();
})
function www()
{
$.ajax({
type: "post",
url: "/Main/GetLoginTimes",
dataType: "json",
success: function (data) {
$("#LoginTime").html(data);
}
});
setTimeout("www()", 1000);
}
</script>
{
try
{
string loginTime = Session["ServerTime"].ToString();
DateTime dateTimeLogin = Convert.ToDateTime(loginTime);
DateTime dateTimeNow = DateTime.Now;
TimeSpan ts = dateTimeNow - dateTimeLogin;
string str = "";
if (ts.Hours > 0)
{
str = ts.Hours.ToString() + "小时 " + ts.Minutes.ToString() + "分钟 " + ts.Seconds + "秒";
}
if (ts.Hours == 0 && ts.Minutes > 0)
{
str = ts.Minutes.ToString() + "分钟 " + ts.Seconds + "秒";
}
if (ts.Hours == 0 && ts.Minutes == 0)
{
str = ts.Seconds + "秒";
}
return Json(str, JsonRequestBehavior.AllowGet);
}
catch (Exception)
{
return Redirect("/Main/Login");
}
}
这样一个简易的计时器就出来了。