Thứ Năm, 3 tháng 5, 2012

How do I make my ASP page pause or 'sleep'?

There is a free component that will help you do this; it is called WaitFor 1.0 and is available at ServerObjects Inc.

If you are using SQL Server (or have access to one), you can try out the following script (adapted from a post by Pierre W):

<%

    Set conn = CreateObject("ADODB.Connection")
    conn.Open ""

    ' indicate a number of seconds, up to 59
    sleep = 10

    ' make sure timeout doesn't expire!
    conn.commandTimeout = sleep + 5

    ' if you neede more than 59 seconds, you will need to adjust the SQL:
    sql = "WAITFOR DELAY '00:00:" & right(clng(sleep),2) & "'"

    Response.Write(now & "")
    conn.Execute sql,,129
    Response.Write(now & "
")

    conn.close:     Set conn = Nothing
%>

Some people will suggest a loop like this:

<%
Response.Buffer = true

Function WaitFor(SecDelay,ShowMsg)
    timeStart = Timer()
    timeEnd = timeStart + SecDelay

    Msg = "Timer started at " & timeStart & "
"
    Msg = Msg & "Script will continue in "

    i = SecDelay
    Do While timeStart < timeEnd
        If i = Int(timeEnd) - Int(timeStart) Then
        Msg = Msg & i
        If i <> 0 Then Msg = Msg & ", "
        If ShowMsg = 1 Then Response.Write Msg
%>

<%         Response.Flush() %>

<%
        Msg = ""
        i = i - 1
        End if
        timeStart = Timer()
    Loop
    Msg = "...
Slept for " & SecDelay & " seconds (" & _
        Timer() & ")"
    If ShowMsg = 1 Then Response.Write Msg
End Function

Call WaitFor(20,0)
Call WaitFor(3,1)
%>

This is BAD, BAD news. If you can't immediately see why this is a problem, run it and watch task manager:


Look at the page faults, and look at the CPU usage for w3wp.exe. And this is on a dual-processor workstation; it literally pegs a single-CPU server at 99% running through that loop.

(http://classicasp.aspfaq.com/general/how-do-i-make-my-asp-page-pause-or-sleep.html)

Không có nhận xét nào:

Đăng nhận xét