Note: This blog post is from 2007. Some content, links and indeed comments from others may be outdated--though not necessarily. Corrections are welcome, in the comments. I may revise the content if necessary.Ever wanted to pause a currently running request? You can now, in CF8, using the new sleep() function. CF8's chock full of hidden gems. Indeed, I make note of nearly 50 of them in a user group talk I've started doing. One of them is this new Sleep() function.
Some will recognize it as an easier way to call the java thread.sleep method (as many have noted, and I wrote about back in 2002). 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".)
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).
Still another reason to use it has to do with another hidden gem, this one in CF7. I'll write about that shortly.
For more like this: