[Looking for Charlie's main web site?]

CF 8 Hidden Gem: new Sleep() function to pause a current request

Note: This blog post is from 2007. Some content may be outdated--though not necessarily. Same with links and subsequent comments from myself or others. Corrections are welcome, in the comments. And I may revise the content as 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:

<cfset sleep(5000)>

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 content like this: Need more help with problems?
  • If you may prefer direct help, rather than digging around here/elsewhere or via comments, I can help via my consulting services
  • See that for more on how I can help a) over the web, safely and securely, b) usually very quickly, c) teaching you as we go, and d) with satisfaction guaranteed
Wow, great tip charlie! I didn't even know they had added that but it could be VERY useful for dealing with Ajax race conditions. That's an issue that I still haven't solved.. let me give an example from my new Project Tracker:

Let's say I make a user an admin of a project - I then redraw the div listing the project admins. Sometimes the database hasn't caught up yet, though.. I need to give it a few milliseconds to get the update in there before requerying the database. This would be a great solution since I could deal with a quarter second delay redrawing that part and that should be plenty of time to let the update go through.

Thanks! The CF8 goodness just keeps coming :)
# Posted By Joe Danziger | 6/15/07 10:35 AM
<cffunction name="sleep" output="No" returntype="void" hint=
   "Pauses execution for a number of milliseconds."
   <cfargument name="milliseconds" required="Yes" type="numeric"/>
   <cfset var thread = CreateObject("java", "java.lang.Thread")/>
   <cfset thread.sleep(ARGUMENTS.milliseconds)/>
# Posted By Darin Kadrioski | 6/15/07 12:19 PM
Darin, did you read all of the blog entry? :-) I said in the opening of the 2nd paragraph that "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). " In the link I offered, I had pretty much the same code.

I do understand that it's not really "new" functionality. It's just simpler (just like the CFLOOP ARRAY--nothing we couldn't do before, just easier).

Anyway, either of our code examples can be helpful to those still running CF7 (or 6, or indeed back to 4.51, when Java integration was first enabled--prior to 6 you had to point CF to the JVM.)

Indeed, Joe, if you're challenges are happening on 7, then do know that you don't NEED to wait for CF8 to solve that problem. Cheers, gents.
# Posted By Charlie Arehart | 6/15/07 12:53 PM

I didn't see your post in 2002, I accredited the find to Peter Freitag when I wrote a function for cflib.org back in 2003:


Glad to see this as a native function. Thanks for blogging it!
# Posted By Nathan Strutz | 6/15/07 1:18 PM
Oh now worries, Nathan. It's so long ago, I can't remember who I'd learned it from. It could have been Pete--he's posted some amazing stuff over the years. :-)

Anyway, we all just keep passing the baton, as we proceed in the race. :-) Thanks, though.
# Posted By Charlie Arehart | 6/15/07 1:36 PM
BlueDragon has had <CFPAUSE interval=""> for years now.

But you knew that already Charlie didn't you?
# Posted By Alan | 6/15/07 2:49 PM
Yep, no slight intended there, Alan. Just wasn't thinking of it as I wrote.

After 15 months (since I left New Atlanta), I guess my memory may be fading for remembering to make such connections whenever I can. Clearly I used to, all the time, and I still do it often (sometimes still more often than many would prefer).

But clearly, it's no longer at the forefront of my attention, and occasionally it may seem to some that I've even turned my back on BD. I have not. I even have clients who use it.

Still, as far as making references to it, it's just one of those things where I'm "damned if I do, and damned if I don't". It ain't easy bein' me. :-)

PS I know you weren't "damning" me, Alan. I'm referring to a much broader context.
# Posted By Charlie Arehart | 6/15/07 3:43 PM

Thanks for sharing about the server monitoring; It is one of my favorite features.

I added your CF8 feed to the Scorpio Yahoo Pipe:

More here:

Dan Wilson
# Posted By Dan Wilson | 6/15/07 10:09 PM
Yep, that's a really cool idea, Dan. I'd seen it before and was looking forward to seeing how it evolves. Thanks for doing it.
# Posted By Charlie Arehart | 6/16/07 11:14 AM
Copyright ©2019 Charlie Arehart
Carehart Logo
BlogCFC was created by Raymond Camden. This blog is running version 5.005.
(Want to validate the html in this page?)

Managed Hosting Services provided by
Managed Dedicated Hosting