[英]How to start with “x” seconds in the Timer
我想打開我的表單,計時器從 10 秒開始,然后是 11 秒,然后是 12 秒,以此類推。
代碼:
public static Stopwatch swTimer;
swTimer = new Stopwatch();
<ImaginaryCode>
SwTimer = 10;
</ImaginaryCode>
swTimer.Start();
執行此操作的簡單方法??
計時器.aspx
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick"></asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server"></asp:Label> seconds passed.
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick"></asp:AsyncPostBackTrigger>
</Triggers>
</asp:UpdatePanel>
</form>
計時器.aspx.cs
private static int _beginSec = 10;
protected void Timer1_Tick(object sender, EventArgs e) {
Label1.Text = _beginSec++.ToString();
}
您不能強制秒表在指定時間啟動。 但是,您可以在測量經過時間時簡單地添加 10,000 毫秒。
Stopwatch swTimer = new Stopwatch();
swTimer.Start();
//Do stuff
swTimer.Stop();
//Elapsed seconds, rounding down to the nearest second.
int seconds = (swTimer.ElapsedMilliseconds + 10000) / 1000;
或者,如果您使用秒數,您可以只添加 10 秒。
Stopwatch swTimer = new Stopwatch();
swTimer.Start();
//Do stuff
swTimer.Stop();
//Elapsed seconds, rounding down to the nearest second.
int seconds = (swTimer.ElapsedMilliseconds / 1000) + 10;
編輯:如果您試圖將時間限制在特定的時間跨度,那么您可能想要使用 Timer 代替(如秒表,但會下降。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.