• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
Oct 26, 2011 Oct 26, 2011

Copy link to clipboard

Copied

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

Views

1.8K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 27, 2011 Oct 27, 2011

Copy link to clipboard

Copied

<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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 27, 2011 Oct 27, 2011

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 27, 2011 Oct 27, 2011

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 27, 2011 Oct 27, 2011

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 27, 2011 Oct 27, 2011

Copy link to clipboard

Copied

The closer to real-time you want it to be the more complicated it becomes, and certainly if you're looking for a system with near-realtime functionality then websites aren't really ideal, you need something with two-way communication like Java or a .NET program. Web pages being what they are, it could easily take a web page more than a second just to get to the server in busy times, let alone run the queries, get back and draw the page.

I'd say that every fifteen seconds would be realistic. Will the admins have anything to do on this page? Or is it purely a visual cue? There's nothing more annoying than having your page refresh as you're trying to work. Assuming it's just a display, then it's easy.

Create two CF pages, signin.cfm and view.cfm, for example. Signin.cfm has a form, and updates a database when someone logs in. View.cfm does a select from the database and displays the logged in users to the page. You then use Javascript on view.cfm to refresh the page every fifteen seconds - this creates a new request to the server, which gets an updated query and achieves what you're hoping for.

I think

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Oct 27, 2011 Oct 27, 2011

Copy link to clipboard

Copied

Or if you want to get really fancy - instead of doing a page refresh (which can get problematic especially if you have interface elements on the page that an administrator would need to interact with), use AJAX to retrieve your database data in JSON format and refresh your login view portion of the page dynamically.  That way the whole page doesn't refresh and only the login data updates dynamically.

Your resources would look something like this:

signin.cfm - signs user into systm

whoIsSignedIn.cfm - retrieves a list of the current users signed in via <cfquery> and outputs results in JSON format

view.cfm - includes JS code that queries the whoIsSignedIn.cfm page asynchronously using AJAX (jQuery!) and updates only the HTML control that includes the log in data - e.g. a table or div tag. 

You can use javascript to schedule your AJAX calls (though you probably want to include code that holds off on a new request if a pending request has not finished) - something like a setTimeout() call inside of your success/failure callbacks.  jQuery has a number of plug ins that will allow you to take that data in JSON format and display it in an HTML control (e.g. Data Tables) that administrators can sort and page through

A little more complicated than the refresh solution, granted.  But a great opportunity to introduce yourself to some great AJAX concepts or jQuery.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 27, 2011 Oct 27, 2011

Copy link to clipboard

Copied

That sounds like an ideal beginner's solution

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 15, 2020 Dec 15, 2020

Copy link to clipboard

Copied

<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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 19, 2020 Dec 19, 2020

Copy link to clipboard

Copied

 

<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>

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 19, 2020 Dec 19, 2020

Copy link to clipboard

Copied

LATEST

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation