[英]Why is the code in Timer Control executed only once?
我寫了以下代碼:
public partial class Default2 : System.Web.UI.Page
{
int time = 60;
protected void Page_Load(object sender, EventArgs e)
{
Label2.Text = "" + time;
}
protected void Timer2_Tick(object sender, EventArgs e)
{
time = time - 1;
Label2.Text = "" + time;
}
}
我正在嘗試類似於倒數計時器的操作。 輸出應打印為“ 60”,然后是“ 59”,然后是“ 58”,依此類推。 但是計時器中的代碼僅執行一次,即輸出先輸出為“ 60”,然后輸出為“ 59”,然后數字停止遞減。
計時器每秒執行一次。 但是int time
不是一成不變的,每次執行計時器時它都會從60開始。 嘗試將DateTime.Now
放入標簽文本中,您會看到Timer不是問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.