Put a pause in your code
So the other day I needed to put a pause in my code. This is something I seem to run into every so often, but this time I decided to put the solution into a custom tag, making it much easier to include in my code. I had previously looked around quite a lot and there are many ways to skin this particular cat, but in this case I turned to Java now that we have CFMX. In particular the threed.sleep() method. It is a method of the Thread class that can create a pause measured in milliseconds. The raw Java code looks like this:
try {
Thread.sleep ( 1000 );
}
catch ( InterruptedException e ) {
}
Now I need to get a quick implementation in the form of a custom tag that I could easily include in my code, providing a way to pass an argument in seconds rather than milliseconds. The resulting code was my new custom tag:
<cfparam name=”seconds” default=”1″>
<cfset seconds = attributes.seconds>
<cfif not(isnumeric(seconds))>
<cfset seconds = 1>
</cfif><cfscript>
thread = createObject(“java”, “java.lang.Thread”);
thread.sleep(javaCast(“long”, 1000*#seconds#));
</cfscript>
Notice I do check to see if the value passed is actually numeric and if not, I just force the tag to default to one second. Now I just call my tag like so:
<cf_pause seconds = 1>
There you have it, a nice pause for your code provided you can keep the tag (pause.cfm in this case) in your CustomTags folder OR in the current working directory.
Comments(0)