只需要在循環中加入SCript.Sleep 100,就可以了。
測試代碼:
- Option Explicit
- On Error Resume Next '測試中最好去掉,否則不能發現錯誤
- Dim Wsh,sProgramme,sRunTime,nTime
- sProgramme="test.bat"
- sRunTime=Mid(Time,4,5)
- nTime=Mid(Time,4,5)
- Msgbox nTime
- Do While True
- WScript.Sleep 1000 '循環中使用這句即可解決
- If nTime=sRunTime Then
- Set Wsh=WScript.CreateObject("WScript.Shell")
- wsh.Run "taskkill /f /im iexplore.exe",0,True
- Wsh.Run sProgramme,0,True
- Set Wsh=NoThing
- sRunTime=DateAdd("h",2,Time)
- sRunTime=Mid(sRunTime,4,5)
- Msgbox sRunTime
- Exit Do
- End If
- nTime=Mid(Time,4,5)
- WScript.Sleep 1000
- Loop