服务器端用 下面的示例创建一个 Timer,它在十秒钟后在控制台上显示“Hello World!”(世界你好!)。 [Visual Basic, C#, C++] 对于此示例,使用 System.Timers 命名空间。 [Visual Basic] Public Class Timer2
Public Shared Sub Main() ' Create a new Timer with Interval set to 10 seconds. Dim aTimer As New System.Timers.Timer(10000) AddHandler aTimer.Elapsed, AddressOf OnTimedEvent ' Only raise the event the first time Interval elapses. aTimer.AutoReset = False aTimer.Enabled = True
Console.WriteLine("Press 'q' to quit the sample.") While Console.Read() <> CInt("q") End While End Sub ' Specify what you want to happen when the event is raised. Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs) Console.WriteLine("Hello World!") End Sub End Class [C#] public class Timer2 {
public static void Main() { // Create a new Timer with Interval set to 10 seconds. System.Timers.Timer aTimer = new System.Timers.Timer(10000); aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent); // Only raise the event the first time Interval elapses. aTimer.AutoReset = false; aTimer.Enabled = true;
Console.WriteLine("Press \'q\' to quit the sample."); while(Console.Read()!='q'); }
// Specify what you want to happen when the event is raised. private static void OnTimedEvent(object source, ElapsedEventArgs e) { Console.WriteLine("Hello World!"); } } 自己改一下.客户端用 javascriptfunction FunctionName() { 要执行的东西 } setInterval("FunctionName()",1000); 后面的时间为毫秒
下面的示例创建一个 Timer,它在十秒钟后在控制台上显示“Hello World!”(世界你好!)。
[Visual Basic, C#, C++] 对于此示例,使用 System.Timers 命名空间。
[Visual Basic]
Public Class Timer2
Public Shared Sub Main()
' Create a new Timer with Interval set to 10 seconds.
Dim aTimer As New System.Timers.Timer(10000)
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
' Only raise the event the first time Interval elapses.
aTimer.AutoReset = False
aTimer.Enabled = True
Console.WriteLine("Press 'q' to quit the sample.")
While Console.Read() <> CInt("q")
End While
End Sub ' Specify what you want to happen when the event is raised.
Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
Console.WriteLine("Hello World!")
End Sub
End Class
[C#]
public class Timer2
{
public static void Main()
{
// Create a new Timer with Interval set to 10 seconds.
System.Timers.Timer aTimer = new System.Timers.Timer(10000);
aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
// Only raise the event the first time Interval elapses.
aTimer.AutoReset = false;
aTimer.Enabled = true;
Console.WriteLine("Press \'q\' to quit the sample.");
while(Console.Read()!='q');
}
// Specify what you want to happen when the event is raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Hello World!");
}
}
自己改一下.客户端用
javascriptfunction FunctionName()
{
要执行的东西
}
setInterval("FunctionName()",1000);
后面的时间为毫秒
{
init();
System.Timers.Timer myTimer=new System.Timers.Timer(3000);
//关联事件
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
myTimer.AutoReset = true ;
myTimer.Enabled = true ;
} private void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//获取当前时间
init();
}
private void init()
{
Application.Lock();
Application["TIMEDEMO"]= DateTime.Now.ToString();
Application.UnLock();
}
function popmsg()
{
}
Global.asax里面就行了.
protected void Application_Start(Object sender, EventArgs e)
{
System.Timers.Timer Timer1 = new System.Timers.Timer();
Application["a"]=DateTime.Now.ToString();
Timer1.Enabled = true;
Timer1.Interval = 5000;
Timer1.Elapsed +=
new System.Timers.ElapsedEventHandler(Timer1_Elapsed);
}
private void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Application["a"]=DateTime.Now.ToString();
StreamWriter sw=File.AppendText(Server.MapPath("3.txt"));
sw.WriteLine(Application["a"]);
sw.Close();
}