Making ColdFusion sleep

Often times you want the server to pause execution of a page, or go to sleep for a short while. You may be waiting for a file to be written to the file system etc.

You can do achieve this in the following fashion:

<cfset createObject('java', 'java.lang.Thread').sleep(5000) />

The example above makes the server call “sleep” for 5 seconds.

Ryan Duckworth states:

“A java thread (which ColdFusion code is converted to) is doing a NOP (no operation) procedure during a java Sleep command which causes no inefficiency. The multi-processing nature of the CPU handles this functionality for you and will use those CPU cycles with other requests during the sleep.”

Note that I usually have this in a Utility component with lots of functions I can call when needed. For example:

<cffunction name="sleep" access="public" output="false" returntype="void" hint="Leverages Java's sleep() function">
   <cfargument name="timeToSleep" type="numeric" required="true" />

   <cfscript>

      createObject("java", "java.lang.Thread").sleep(arguments.timeToSleep);	//sleep time in milliseconds
      return;

   </cfscript>

</cffunction>

So then I can call application.utility.sleep(1000);

:)

Post a Comment or Leave a Trackback

2 Comments

  1. Simeon
    October 24, 2007 at 1:54 am | Permalink

    in CF8 there is now a Slep function which is identical to this.

  2. October 24, 2007 at 2:35 am | Permalink

    Yep, CF8 is such a great release with a bunch of goodies!

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*