How to Make ColdFusion Sleep

From time to time, you need to make your web app pause for a number of milliseconds or seconds. The need isn't often, but it does come up. There is no ColdFusion function or tag that accomplishes this currently, but you can do it with the following code snippet:

<cfscript>
    thread = CreateObject("java", "java.lang.Thread");
    thread.sleep(4000);
<cfscript>

This example will sleep for 4000 milliseconds, or 4 seconds.

This isn't news. There are many blogs out there that mention this extremely useful tidbit. Again, I don't mean to add to the echo chamber; I just use my blog as my initial reference point for little technical tidbits, so I had to throw it in there.

This is useful when ColdFusion needs to wait for a task to complete. For instance, I'm working with Active Directory today, and if I change an account in the directory with a DSMod command and then immediately make an LDAP call to load that account's updated information, the directory doesn't have time to react to the change and sends the old information in the LDAP query. If ColdFusion pauses about 2 seconds before querying LDAP, however, the updated information will come through, because the directory has time to catch up with the DSMod command. This is just one example of the usefulness of "putting ColdFusion to sleep".

One Response to “How to Make ColdFusion Sleep”

  1. coldfusion pause Says:

    […] however, the updated information will come through, because the directory has time to catch up …http://blog.crankybit.com/how-to-make-coldfusion-sleep/Stylesheet Debugging Tips COLDFUSION DEVELOPER’S JOURNALWhen your XSLT stylesheet doesn’t do exactly […]

Leave a Reply

  Theme Brought to you by Directory Journal and Elegant Directory.