Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you create a delay in asp

Status
Not open for further replies.

xwb

Programmer
Jul 11, 2002
6,828
GB
I'm trying to simulate a Comet (reverse Ajax) type situation where the server pushes data. I don't want to push the data all at once: more in drips and drabs with random delays in between. Something to simulate data coming from a real device. I can figure out how to do everything else except put in a delay in the server. setTimeout doesn't work, neither does sleep.

Any idea how I can create a delay in the server without being driven by the client?
 
if you have a dedicated server and it doesnt have to scale up for multiple users then perhaps turning off the response buffer, response.flush some data, counting to ten million, response.flush more data, counting to 2 million, response.flush more, and so on.
 
Thanks - I'll try that. IIS is on localhost.
 
If you know how slow you want the info to appear then put in a giant loop.

for x = 0 to rs.recordcount

dim whoo
whoo = rs.fields(x).value
dim y
y = 0
for y = 0 to 250000
next
next

Some how I dout this is what you want, but...
 
Mm... giant loops are usually not a good idea. In the "old" days of programming, when you knew specifically what machine and processor that you were writing for, you could kill some cycles. But "giant loop" delays on today's system don't do much good; and if you *do* manage to slow things down, you're eating up cycles that are used for the other processes in Windows.

I would be more inclined to use a Javascript routine with a timer, pumped out by ASP.

Remember, ASP is running on the SERVER. It's going to (generally) do it's thing, then pump the data out (depending on your response.buffer setting) all at once, making the loop pointless. You'll just time out your browser waiting for it to get *anything*.



Just my 2¢

"What the captain doesn't realize is that we've secretly exchanged his dilithium crystals for new Folger's Crystals." -- My Sister
--Greg
 
You could import (define) the win32 sleep command and use that. Generally any kind of purposeful delay is not a great idea for a web page though.

 
I agree it is much better to have the browser check for updates than to slow the server's processing of the HTTP Response.
 
I'm just trying to simulate a slow server which injects data when in arrives instead of the client polling the server every so many seconds. Basically want to do is to

1) Keep the connection open
2) Send data out at irregular intervals

The problem is putting in a little delay in between the irregular intervals. I can do it with PHP and Perl but I can't figure out how to do it with Javascript or VBScript.

This is a standalone system which only has one client: me. Slowing down the service isn't a problem.
 
Another option is to (mis)use the Popup method of the WScript.Shell object by setting the timeout value in the call. Code execution will pause until someone clicks the ok button (heh) or the timeout passes.

 
Problem is WScript is not accessible from asp. setTimeout doesn't work either.
 
I think WScript is accessible, although there is the possibility I changed a setting a year ago when I set up this machine and dont remember now :p

This works fine on my system:
Code:
<%
Option Explicit

'Turn Off Buffer
Response.Buffer = False

'Create WScript.Shell object
Dim objShell
Set objShell = Server.CreateObject("WScript.Shell")

'Seed the randomizer
Randomize

'get the start time
Dim start_time
start_time = timer

'spit out data with random pause ten times
Dim i
For i = 0 To 9
   objShell.Popup "Fake Wait",Round(Rnd() * 5)
   Response.Write "Random Data #" & i & " | Elapsed: " & (timer - start_time) & "<br/>"
Next

Set objShell = Nothing
%>

I used a maximum 5 second delay because I didn't want to wait. I used the timer to help me track that the popup was really working. Since I am running this in a web site that uses the default IIS user to run the site, I don't even see the popup because I am logged in as a differant user.

-T

 
For cpu-friendly wait on server, you can do either.

[1] One can install meadroid.com's sriptx so that you can call its activex dll's method wait.
[tt]
server.createobject("scriptx.factory").wait 2000 'unit ms, 2sec delay
[/tt]
[1.1] Alternaive: write your own activex dll.
[1.2] Any of the existing free asp component for delay out there.

[2] If one feels uneasy to install 3rd party component, one can make use of win os ping.exe to form an synthetic wait by pinging a nonexisting ip address.
[tt]
createobject("wscript.shell").run "%comspec% /c ping -n 2 -w 1000 256.0.0.1 >nul",0,true '2 sec delay on the paper
[/tt]
But the error margin is large for O(second) delay due to the overhead. Hence for sub-second delay or even <10 sec delay, it certainly is not accurate but only figuratively doing a delay job. (The [1]'s error is 10ms order of magnitude.)
 
Code:
seconds = 0.5
set oShell = CreateObject("Wscript.Shell")
cmd = "%COMSPEC% /c ping -n " & 1 + seconds & " 127.0.0.1>nul"
oShell.Run cmd,0,1

George
 
Many thanks for the delay routines
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top