Skip to main content
Participant
October 27, 2011
Question

How to loop to run a piece of code every second?

  • October 27, 2011
  • 3 replies
  • 2236 views

Can anyone show me a simple Coldfusion code to loop to run a piece of code every second? Thank you so much.

This topic has been closed for replies.

3 replies

Charlie Arehart
Community Expert
Community Expert
December 19, 2020

Eddy, considering the later clarification you've made, rather than worry about any code that loops or waits, this sounds more like a problem perhaps better solved with websockets (introduced in cf11). While nearly all demos focus on chat, the point is that two things are connected to each other, to respond to each other's events asynchronously.

 

Just Google for coldfusion websockets and see if some of the examples might connect the dots for you. Or look also  just for resources on websockets WITHOUT respect to cf. The concepts apply regardless of implementation.

 

Let us know if you may consider it or not. 

/Charlie (troubleshooter, carehart. org)
Participant
December 15, 2020
<cfsetting requestTimeOut="999999999">
<CFSCRIPT>
    totalStartTime = getTickCount()
    x = 0;
    time = 0;
    while (x LT 300) { 

        startTime = getTickCount()
        // block of code to time here

        //where code would run and do its thing
            sleep(randRange( 50, 800, "SHA1PRNG" ));
        //end where code would run and do its thing 

        executionTime = getTickCount() - startTime;

        // statements 
        x = x +1;

        if (executionTime < 999) { 
            waitTime = 999 - executionTime;
            sleep(waitTime);
            }
       // WriteOutput("#x#-#waitTime#, ");
       // cfflush();
      }

      totalExecutionTime = getTickCount() - totalStartTime;
      WriteOutput("
        <P>final time: <br>
            #totalExecutionTime#<br>
            #x#
        </P>
        ");
</CFSCRIPT>
BKBK
Community Expert
Community Expert
December 19, 2020

 

<cfscript>
	// 1 minute in milliseconds
	howLongToRunCodeForInMillis=60000;
	
	// start the clock
	startTimeInMillis=getTickCount();
	elapsedTimeInMillis=0;
	perSecondCounter=1;
	
	//loop to control the time
	While (elapsedTimeInMillis lte howLongToRunCodeForInMillis) {	
		if (elapsedTimeInMillis gte 1000*perSecondCounter) {
			// put in here the code you wish to run every second	
			//writeoutput("elapsedTimeInMillis:" & elapsedTimeInMillis & "<br>");	
			perSecondCounter=perSecondCounter+1;	
		}
			
		// when to stop
		if (elapsedTimeInMillis gt howLongToRunCodeForInMillis) {
			break;			
		}
		
		elapsedTimeInMillis=getTickCount()-startTimeInMillis;
	}
</cfscript>

 

Owainnorth
Inspiring
October 27, 2011

<cfloop condition="true">

  <!--- do something --->

  <cfset sleep(1000) />

</cfloop>

It's not without its caveats mind, you've essentially got one thread running forever, or more accurately until it reaches the Maximum Thread timeout in CFAdmin. As CF only uses about ten threads in total by default, you're essentially tying up 10% of your CPU resources.

I would imagine ColdFusion is not the tool for the job here, care to elaborate?

Participant
October 27, 2011

Thank you so much. Can I set a loop count but how to start again after exit the loop? I tried JavaScript, but the cfquery will get what I need until the whole page refresh, example:

<script language="javascript">

function refresh ()

{

<cfquery name="tquery" datasource="xxx">

    Select query....

</cfquery>

<cfif tquery.Status EQ "Refresh">

    <cfquery name="query" datasource="xxx">

        Set query....

    </cfquery>

    window.location.reload();

</cfif>

}

Any ideas?

setInterval ('refresh();',1000);

</script>

Participant
October 27, 2011

With all due respect, I think you need to get more experience with CF and JS first, as you've made the classic mistake everyone makes.

Javascript is *client-side* - it runs inside the user's browser, and has no connection with the server which served up the page. ColdFusion is *server-side* - it runs before a single byte is returned to the browser.

What you're trying to do in this method is nonsensical - you cannot call ColdFusion code from Javascript. They do not know about each other, and cannot directly communicate with each other either. There are ways to do what you're trying, but they've quite advanced - take a look at CF Ajax functionality if you really want to know

What is it you're actually trying to achieve? Having a browser refresh every second would be a nightmare from a user's perspective.


Thank you for your quick reply. I thought this will be a simple question, but it seen like hard to find a fast and simple solution. I am working on an online sign in tool. Two browser windows are open at the same time. One is for people to sign in with password and submit their information; another one is for Administrator to see who sign in. I am trying to refresh the Administrator page whenever user finished sign in and submitted their information. These two pages cannot be parent and child page, and will be open in multiple computers. I am new to Ajax. What will be the easiest way to solve this in Ajax? Thank you so much for your time. Have a wonderful week.