Private m_LastCheck As Variant '上一次触发的时间
Private m_Interval As Long '时间间隔
Private Sub Form_Load()
'每三秒触发一次
m_Interval = 3
End Sub
Private Sub Timer1_Timer()
If IsEmpty(m_LastCheck) Then
m_LastCheck = Now
Else
'以秒为单位,也可以是分钟、小时、日、月等有效的时间单位,只需要修改DateDiff函数的第一个参数。
If Abs(DateDiff("s", Now, m_LastCheck)) >= m_Interval Then
Debug.Print "OK"
m_LastCheck = Now
End If
End If
可以调用 API 得到长延时的 Timer (长达 47 天)
'模块中代码
Dim lTimerId As Long
Private Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
Private Sub TimerProc(ByVal lHwnd As Long, ByVal lMsg As Long, ByVal lTimerId As Long, ByVal lTime As Long)
Dim lResult As Long
lResult = StopTimer(lTimerId)
'以下放入到达延时后执行的代码
Call InsertYourProcessNameHere
End Sub
Public Sub StartTimer(lInterval As Long) '传送前将延时间隔转换为毫秒
lTimerId = SetTimer(0, 0, lInterval, AddressOf TimerProc)
End Sub
Public Function StopTimer(lTimerId As Long) As Long
'must pass the TimerId returned by SetTimer
StopTimer = KillTimer(0, lTimerId)
End Function