Skip to main content
Participant
December 27, 2006
Question

delay before cflocation

  • December 27, 2006
  • 4 replies
  • 3316 views
I would like to have a delay in order to display some text before calling cflocation.

<cfif #URL.option# EQ 'REPORT'>
Running Report<br>
<cfloop index = "LoopCount" from = "1" to = "8000000"></cfloop> <!--- any better way to create a delay?? --->
<cflocation url="remit_action.cfm?id=#URL.id#&source=#URL.source#&option=#URL.option#">
</cfif>

It appears the cf 'builds' the whole page before being displayed, therefore, when it hits the cflocation it simply moves on to the next page.

What I really need is do display a message, such as "running report", until the file exists. I can use cfif FileExists alright, but the delay has me stumped.
    This topic has been closed for replies.

    4 replies

    Inspiring
    December 27, 2006
    Ian,
    that static sleep method is pretty neat. I keep forgetting that we have access to all the standard java objects now with CFMX.
    Inspiring
    December 27, 2006
    You can't ahve a <cfflush> before a <cflocation>. Nor can you output
    anything on the browser prior to a <cflocation>.

    <cflocation> sends a 302 redirect code to the browser, which tells the
    browser "go to this URL instead". The browser does that. It does not
    render any body of the response once it sees the 302 in the header.

    You can effect the same sort of thing, though, by doing this:
    - output some stuff
    - <cfflush>
    - Use java.lang.Thread's sleep() method to pause for a bit (note, this
    pause occurs on the SERVER not the browser, but it can amount to the same
    thing).
    - Use Javascript to relocate to a different URL.

    Or you can use a <meta> tag to "refresh" the browser to a new URL after a
    period of time. This is perhaps better, as it all happens on the browser,
    whereas the steps above are a bit of a mishmash.

    --
    Adam
    neal77Author
    Participant
    December 28, 2006
    Thanks for the responses. You are correct; cannot use a CFFLUSH before a CFLOCATION.
    I settled for this;

    <cfset thread = CreateObject("java", "java.lang.Thread")>
    <cfset report = 1>
    <cfflush>
    <cfflush interval="2">
    running
    <cfloop condition = "report EQ 1">
    <cfset thread.sleep(8000)>
    <cfif FileExists ("\\exp-app1\EXPERIOR\RUN\STATUS\ACTIVE\EDIRPTREMITPOI")>
    ...
    <cfelse>
    <cfset report = 0>
    done<br>
    </cfif>
    </cfloop>
    <cfset thread.sleep(4000)>
    return to <a href="remit_list.cfm">remit list</a>

    I wanted to avoid an anchor tag, but it will do.
    Inspiring
    December 27, 2006
    Check out <cfflush...> in the docs. This allows CF to stream the
    response back to the client, rather then wait until the entire response
    is complete. But it does have limitations and conflicts with other tags
    because of the way it changes the response behavior.

    You may also want to be aware of the ability to put CF to sleep for
    awhile. I tiny bit of Java can be employed to pause the processing of a
    thread by CF.

    <cfset thread = CreateObject("java", "java.lang.Thread")>
    About to sleep for 5 seconds...<cfflush>
    <cfset thread.sleep(5000)>
    Done sleeping.

    How to make ColdFusion MX go to sleep
    http://www.petefreitag.com/item/85.cfm
    Many people have had the need for a ColdFusion page to sleep, typically
    between iterations of a loop. There is a tag called CFX_Sleep in the Tag
    Gallery, but in ColdFusion MX you don't need a CFX tag to make the
    current processing thread sleep using the static sleep method on the
    java.lang.Thread class, part of the standard java platform. Because CFMX
    doesn't allow us to call a static method without an object reference, we
    have to first use CreateObject, or CFOBJECT to get an instance of a
    java.lang.Thread object. We then call the Thread.sleep(long) method,
    which takes in the number of miliseconds to sleep for.
    Inspiring
    December 27, 2006
    Why not pop up a javascript window when you submit your report form that has your "running report" message and then close it when your report script finishes running?

    Otherwise, you could experiment using <cfflush>, but it sounds like cfflush has problems when you use other CF methods that alter the HTTP headers (i.e. cflocation).

    If you just want CF to delay until a file exists, you could do something like this:

    <cfloop from="1" to="80000" index="i">
    <cfif FileExists("C:\Some\File\Path.ext")>
    <cfbreak>
    </cfif>
    </cfloop>
    <cflocation url="MyFileLocation.cfm">