Jump to content

Need the correct if statement please?


Padgoi

Recommended Posts

Hi everyone.  So I'm using this javascript slide down effect and it works fine and it's updating every 0.2 seconds (as the duration indicates) but it's updating constantly, not just when the last post is made.  I only want it to slide-down when the last post is actually made, right now it's sliding down every 0.2 seconds regardless of whether a new post is made or not.  I know it's the if statement I have wrong.  Can someone help me out with this?  Thanks in advance. 

 

Here's the code:

 

var c=0
var t
function ajaxLastPost()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
else
  {
  alert("Your browser does not support XMLHTTP!");
  }
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
  {
  document.getElementById('DivLastPost').innerHTML=xmlhttp.responseText;
  Effect.SlideDown( 'DivLastPost', {duration: 0.1} );
  }
}
xmlhttp.open("GET","sources/action_public/lastpost.php",true);
xmlhttp.send(null);
c=c+1;
t = setTimeout("ajaxLastPost()",5000);
}

Link to comment
Share on other sites

I'm guessing you'd need to compare the result you're getting to that of what's currently in there.

something like:

if(xmlhttp.readyState==4)
  {
    //Only slide down if something actually changed.
    if( xmlhttp.responseText != document.getElementById('DivLastPost').innerHTML )
    {  
      document.getElementById('DivLastPost').innerHTML=xmlhttp.responseText;
      Effect.SlideDown( 'DivLastPost', {duration: 0.1} );
    }
  }

 

edit thinking about that again it doesn't make much sense :),

if( xmlhttp.responseText != document.getElementById('DivLastPost').innerHTML )

that bit needs to be compared to the last result on the currentpage, prolly some value you want to set when the page loads.

Link to comment
Share on other sites

I don't know what Effect.SlideDown() does. I assume it doesn't have a continuous effect, right?

 

And why did you put a setTimeout in the ajaxLastPost function to call itself? That would just keep running indefinitely. You probably want to make an if statement to check if the last post is submitted (not sure how you do that) and if so, run the AJAX stuff. If not, don't.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.