我正在尝试创建一个15分钟的计时器,当用户单击一个按钮以“签出”或锁定案件时,它将启动一个计时器,该计时器在15分钟内运行一项操作,该操作将翻转我的数据库中的布尔开关,从而将其解锁15分钟后再次发生这种情况。我假设这需要在服务器端代码而不是Javascript上完成,因为如果有人离开该页面将无法运行。我希望可以在操作方法中插入一些可以实现此目的的方法。我已经研究过,但是找不到如何解决的确切答案。任何帮助将非常感激。 using (Html.BeginForm("CheckoutCase", "Case")) { @Html.HiddenFor(x => x.ID) <input type="submit" value="Checkout" name="submitAction" class="btn btn-block alert-success"/> }控制者[HttpPost] public ActionResult CheckoutCase(int id) { Case currentCase = db.Cases.Find(id); currentCase.LockCase = true; currentCase.Lockout_TS = DateTime.Now; db.SaveChanges(); string url = this.Request.UrlReferrer.AbsolutePath; return Redirect(url); } 最佳答案 假设currentCase.Lockout_TS保留DateTime标记为currentCase的状态,并且锁定案例时,所有用户均被锁定,为什么不使用?//if currentCase is locked...if (currentCase.LockCase){ //...check if it's been more than 15 minutes since currentCase was locked bool shouldUnlock = (DateTime.Now - currentCase.Lockout_TS).TotalMinutes > 15; //if yes, then unlock it if (shouldUnlock) { currentCase.LockCase = false; //persist the changes and proceed accordingly } //if we've reached this point, shouldUnlock was false _ //which means that currentCase shouldn't be unlocked yet _ //so proceed accordingly}else{ //currentCase is not locked, proceed accordingly} 07-26 04:42