質問

Im running with Coldfusion 11. I have an instant messenger that uses a meta refresh that refreshes the page every 30 seconds to pull new IMs. The issue with this is the flash of the page reload and i would really like it to refresh every 15 seconds. I decided to try to change up and go with looping to check for any unseen messages. If there is one it would then reload the page. If not i want it to sleep for 15 seconds before continuing the loop. As you can see i've set it to do so 40 times. At 15 second intervals it would time out at 10 minutes. If a user has not posted a new message or changed the page at that point we consider them logged out. The code below is at the end of the page. The issue i'm having is that no messages show until the cloop completes. Is there a way to set the loop to run in the background yet display current messages?

<cfloop from="1" to="40" index="i">
<cfset sleep(15000)> 
<cfquery name="messck" datasource="mysource" maxrows="1">
SELECT id
FROM messages
WHERE user = '#getuser.code#' AND friend = '#getme.code#' AND seendate IS NULL
ORDER BY ID DESC
</cfquery>
<cfif messck.recordcount EQ 0>
<cfelse>
<cflocation url="messagebox.cfm?code=#url.code#">
</cfif>
</cfloop>

Thanks, Mike

役に立ちましたか?

解決

I decided to go with some JQuery with Ajax function and it solved my problem perfectly. I included a couple script links to the page displaying the messages, messages.cfm

<script language="javascript" src="http://equifax.ipushjobs.com/jquery/jquery-1.2.6.min.js"></script>
<script language="javascript" src="http://equifax.ipushjobs.com/jquery/jquery.timers-1.0.0.js"></script>

Then added this script.

<script language="javascript">
$(document).ready(function(){
  var j = jQuery.noConflict();
j(document).ready(function()
{
    j(".refreshMe").everyTime(5000,function(i){
        j.ajax({
          url: "messck.cfm?code=#getuser.code#",
          cache: false,
          success: function(html){
            j(".refreshMe").html(html);
          }
        })
    })
});
  j('.refreshMe').css({color:"red"});
});
</script>

Next i placed an empty div at the end of the page.

<div class="refreshMe"></div>

This is my messck.cfm It checks for a new message from the user. If there is a new message it causes the page to refresh.

<cfquery name="ckfornew" datasource="mysource">
SELECT code
FROM messages
WHERE code = '#url.code#' AND friend = '#session.code#' AND seendate IS NULL
ORDER BY ID DESC
</cfquery>
<cfif ckfornew.recordcount EQ 0>
<cfelse>
<cfoutput>
<meta http-equiv=Refresh content="0;url=messages.cfm?code=#ckfornew.code#">
</cfoutput>
</cfif>

I appreciate the input and direction!

他のヒント

You should be able to use <cfflush> to periodically return content from the server.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top