Skip to main content
Inspiring
September 12, 2011
Question

Slow down cfloop query -Is there a way to slow down the cfloop query?

  • September 12, 2011
  • 5 replies
  • 3167 views

I have a query

   <cfquery name="score" datasource="cdb" cachedwithin="#createTimespan(0,0,0,0)#">
      SELECT * FROM cds_master WHERE gw =3 
      </cfquery>

I want to loop through and output the results, but when I do it loops so quickly you cant even see the output.

Is there a way to slow down the cfloop query?

Here is the current code

<cfloop query="score">

<cfoutput>#data#</cfoutput>

</cfloop>

Proposed

<cfloop query="score">

<cfoutput>#data#</cfoutput>....hesitate 10 seconds

</cfloop>

    This topic has been closed for replies.

    5 replies

    Inspiring
    September 12, 2011

    I want to loop through and output the results, but when I

    do it loops so quickly you cant even see the output.

    Something does not add up. Regardless of how long a cfloop takes to generate the results, you would still be able to view the results in your browser. Unless there is more going on in the code than what you posted ...

    Legend
    September 12, 2011

    I hope this "slow logic" is only for your development/test environment otherwise I would strongly suggest rethinking the problem and solution. Slowing down a page like this via sleep or somehting else burns a processing thread on the server which will slow down the entire server if it is a highly used server. You want to get your page processed and completed ASAP.

    If development or testing, consider cfdump or cflog. If production, consider ajax or some other feedback method.

    Inspiring
    September 12, 2011

    These observations have nothing to do with your question, but, in case you are interested,

    First, this is inefficient:

    <cfloop query="score">

    <cfoutput>#data#</cfoutput>

    </cfloop>

    because you are processing the cfoutput tags inside a loop.  This would be more efficient:

    <cfoutput>

    <cfloop query="score">

    #data#

    </cfloop>

    </cfoutput>

    This would be just as efficient, but with less code.

    <cfoutput query="score">

    #data#

    </cfoutput>

    Next, this snippet does not appear to accomplish anything:

    cachedwithin="#createTimespan(0,0,0,0)#"

    What did you have in mind when you wrote it?

    September 12, 2011

    Slow down a loop ? You're suppose to see all the output with that code... If you want to make the score appear with a 10 seconds interval (animation) think about creating a array of score and display them with javascript...

    Sean Coyne
    Participating Frequently
    September 12, 2011

    Unless you are using CFFLUSH to send the content to the browser before the page has completed, the entire page is processed and then sent to the browser as a whole.

    You can use the cfflush tag within the cfloop to push out content as it is processed.

    <cfloop query="score">

    <cfoutput>#data#</cfoutput>

    <cfflush />

    </cfloop>

    If you really need to artificially slow down the processing of the page, you can use the sleep() function.  So if you wanted to wait 10 seconds you would do the following:

    <cfloop query="score">

    <cfoutput>#data#</cfoutput>

    <cfflush />

    <cfset sleep(10000) />

    </cfloop>

    Typically this is only done on long running pages to give the user some feedback, if your "problem" is that the page loads so quickly then, in my opinion, you don't really have a problem, but if you need these two things, or a combination of both, should give you what you are looking for.

    weezerboyAuthor
    Inspiring
    September 12, 2011

    sleep throws an error.

    Variable SLEEP is undefined.

    Inspiring
    September 12, 2011

    sleep throws an error.

    Variable SLEEP is undefined.

    This is why reading & following the "Before you post" guidelines is a helpful thing to do:
    http://forums.adobe.com/thread/607238

    What version of CF are you on?  I am guessing <= CFMX7.

    You can do this instead:

    createObject("java", "java.lang.Thread").sleep(1000);

    But... really... why are you wanting to pause for TEN SECONDS here?  10sec is an eternity as far as web pages go.

    What's the result you're trying to achieve (and don't just say "pausing for 10sec", I get that ;-)

    --

    Adam