VBA,實現延時自動執行的各種方法

2021-10-03 21:47:54 字數 3477 閱讀 2278

doevents( )   轉讓控制權,以便讓作業系統處理其它的事件。

sub test_print1()

for i = 1 to 10

debug.print i

delay30 (2)

next

end sub

sub delay30(t as single)

dim time1 as single

time1 = timer

dodoevents

time2 = timer - time1

if time2 < 0 then time2 = time2 + 86400

loop while time2 < t

end sub

sub test_print11()

for i = 1 to 10

debug.print i

time1 = timer()

dodoevents

time2 = timer() - time1

if time2 < 0 then time2 = time2 + 86400

loop while time2 < 2

next

end sub

declare sub sleep lib "kernel32" (byval dwmilliseconds as long)

sub test_t1()

for i = 1 to 10

debug.print i

sleep 2000

next

end sub

declare sub sleep lib "kernel32" (byval dwmilliseconds as long)

sub test_t2()

for i = 1 to 10

debug.print i

delay_t2 (3000)

next

end sub

function delay_t2(t)

sleep (t)

end function

declare sub sleep lib "kernel32" (byval dwmilliseconds as long)

sub test_t2()

for i = 1 to 10

debug.print i

delay_t2 (3000)

next

end sub

function delay_t2(t)

sleep (t)

doevents

end function

private declare function timegettime lib "winmm.dll" () as long

sub test_print1()

for i = 1 to 10

debug.print i

delay1 (2000)

next

end sub

sub delay1(t as long)

dim time1 as long

time1 = timegettime

dodoevents

loop while timegettime - time1 < t

end sub

private declare function timegettime lib "winmm.dll" () as long

sub test_print1()

for i = 1 to 10

debug.print i

delay1 (2000)

next

end sub

sub delay1(t as long)

dim time1 as long

time1 = timegettime

do' doevents '如果不加doevents 就會像sleep一樣,總卡著等待

loop while timegettime - time1 < t

end sub

sub test_print1()

for i = 1 to 10

debug.print i

doevents

next

end sub

sub test_print1()

for i = 1 to 10

debug.print i

time1 = now() '中間變數固定下當時的時間

dodoevents

loop while now() < time1 + timevalue("00:00:03")

next

end sub

sub test_print12()

for i = 1 to 10

debug.print i

next

end sub

sub test_print13()

for i = 1 to 10

next

end sub

sub t12()

debug.print i

i = i + 1

end sub

sub test_print1()

for i = 1 to 10

debug.print i

time1 = now() '中間變數固定下當時的時間

dodoevents

loop while now() < time1 + timevalue("00:00:02")

next

end sub

也可以寫出只用 doevents +時間的專門的delay() 函式或過程

sub test_print11()

for i = 1 to 10

debug.print i

test_delay5 (3)

next

end sub

sub test_delay5(t)

time1 = now()

dodoevents

loop while now() < time1 + timevalue("00:00:" & t) '只允許設定延遲秒數

end sub

msgbox 或 其他倒計時的

加這個延時函式一起使用

4 實現倒計時視窗,倒計時顯示且自動關閉

重新延時執行的Js 實現

1.autocomplete 外掛程式,當使用者的輸入空閒0.5s 時,才向服務傳送請求。而不是使用者輸入每乙個字元都要請求伺服器。2.懶載入時,使用者拖動滾動條空閒0.5s時,才遍歷懶載入的img元素,這樣操作比較平滑。對每乙個操作,定義乙個唯一操作碼,重新延時執行時,清空該操作碼的執行體。重新定...

VC實現程式的自動執行

很容易實現程式的開機自動執行,在登錄檔中寫入相關資訊即可.在hkey local machine分支下的software microsoft windows currentversion run下寫入 字串鍵值.hkey hkey char currentpath max path char sys...

IDEA JRebel實現全自動熱部署的方法步驟

簡述 jrebel是一款jvm外掛程式,它使得j a 修改後不用重啟系統,立即生效。idea上原生是不支援熱部署的,一般更新了 j a 檔案後要手動重啟 tomcat 伺服器,才能生效,特別影響開發效率。目前對於idea熱部署最好的解決方案就是安裝jrebel外掛程式。安裝jrebel idea 依...