Add Comment
A Basic sleep() Tutorial
ColdFusion Tutorial #23
Ever wanted to pause a currently running request? You can now, in CF8, using the new sleep() function.
Some will recognize it as an easier way to call the java thread.sleep method. It's been added primarily as part of the multi-threaded processing (CFTHREAD) feature, such as when one thread needs to wait upon another.
But it can be useful sometimes on its own, such as when you want to simulate a long-running request for any reason. (And it's a whole lot more server-friendly than doing a huge cfloop, since a sleep call doesn't really spin the CPU. It literally halts the current request, putting it to "sleep".)
demo.cfm
As with the sleep method in java, sleep() takes a number in milliseconds, so for 5 seconds, use 5000. You can use it in CFSCRIPT, or in a simple CFSET.Why might you want to simulate a long-running request? There are many reasons. Perhaps to test some logic in how CFLOCK failures work, or to cause a page to appear in the new CF8 server monitor--or one of the long-existing monitor tools (to make sure you're seeing what you think you should be seeing).
This example simply displays the current time, sleeps for 5 seconds and then displays the time again.
Demo
See this code running!
Download
Download this code as a zip!
Comments
There are no comments!Click button to add a comment
Author
Charlie Arehart
Published
Sunday 02 Dec 2007Original
This tutorial has been modified and published with permission of the author. The original tutorial can be found herehttp://carehart.org/blog/client/index.cfm/2007/6/15/cf8_hidden_gem_sleep_function