Jump to content

auto update


slyths

Recommended Posts

hello everyone,

 

First let me say i have 0 ajax/java experiance. But here is my question....

 

 

I want to make a div on my website auto refresh every 5 seconds. I have searched google, tryed a lot of diffrent things but nothing worked so far. Here is the div i am trying to make auto refresh:

 

Code:

 

<body><div id="hits" align="center"><SCRIPT LANGUAGE="Javascript" SRC="http://xxx.xxxxxx.xxx/counter.php?page=home.html"><!--
//--></SCRIPT></body></div></html>

 

 

 

Note: This is a hit counter, and all i want it to do is to refresh and show if more people viewed this page without having to refresh the whole webpage

Link to comment
Share on other sites

several thoughts...

 

one is that you should be careful wiht this...refreshing the content every 5 seconds is PROBABLY going to be a bit overkill unless you are really expecting LOTS of traffic on the website.  This will simply lead to extra unnecessary overhead on your server and bandwidth.  If you really do have hundreds of new hits every minute, maybe you should program a "fake" hit counter that starts wiht your current number and increments it locally on its own based on the average rates of new visitors to your site.  It could check in with the server every couple minutes to adjust as needed in case the numbers are not jiving perfectly.

 

two...the </div> tag needs to be closed INSIDE the body section...rather than outside it:

...<body><div>content in here</div></body>...

 

three...your javascript should be placed outside the div, as you will be replacing the entire contents of the div

 

four...you can set a timer in your javascript using:

setTimeout(expression, msec);

your 5-second example would be 5000 milliseconds (the second parameter)

 

five...you will want to find a basic/generic ajax function (there are thousands of tutorials on implimenting ajax...just Google that) to call as the expression, and then replace this DIV's contents with the returned value from the AJAX request

 

I hope this helps.

Link to comment
Share on other sites

well...all you need to do is google "AJAX Tutorial" to uncover hundreds of basic scripts to handle an ajax call...here is one that i have used in teh past:

 

   <script language=\"javascript\" type=\"text/javascript\">
     <!-- 
     //Browser Support Code
     function ajaxFunction()
     {
          var ajaxRequest;  // The variable that makes Ajax possible!
          
          try
          {
               // Opera 8.0+, Firefox, Safari
               ajaxRequest = new XMLHttpRequest();
          }
          catch (e)
          {
               // Internet Explorer Browsers
               try
               {
                    ajaxRequest = new ActiveXObject(\"Msxml2.XMLHTTP\");
               }
               catch (e)
               {
                    try
                    {
                         ajaxRequest = new ActiveXObject(\"Microsoft.XMLHTTP\");
                    }
                    catch (e)
                    {
                         // Something went wrong
                         alert(\"Your browser broke!\");
                         return false;
                    }
               }
          }
          
          // Create a function that will receive data sent from the server
          ajaxRequest.onreadystatechange = function()
          {
               if(ajaxRequest.readyState == 4)
               {
                               
                         // the next line inserts retrieved data into a DIV area called <AjaxContentDiv>...change this to whatever the ID of your DIV is
                         document.getElementById('AjaxContentDiv').innerHTML=ajaxRequest.responseText;
                    
               }
          }
          
          var lookupRequest = 'page/to/call/here.php';
          ajaxRequest.open(\"GET\", lookupRequest, true);                  
          ajaxRequest.send(null); 
     }
     
     //-->
   </script>

 

There are two parts of this code to modify:

1. the name of your <div> (in this case, change "AjaxContentDiv" to "hits")

2. the URL of the script that returns your value ( var lookupRequest = 'page/to/call/here.php'; )

 

 

Other thoughts:

I still think that even with 2000 visitors each day (which you may want to check out Google Analytics because you probably have a skewed view of your "real" traffic if you are managing a website with that much traffic but don't know the basics of modern web development), you are going to ruin the good benefits of ajax by requesting updates every 5 seconds...this is not what ajax was developed for and will product tremendous server and client overhead.

 

On top of that, if you do the math...

2000 per day / 24 hours = 83 per hour

83 per hour / 60 minutes = 1.3 hits per minute

This would indicate that refreshing the hit count once per minute should be sufficient...or at the most once every 45 seconds.

 

Obviously you can (and will) do whatever you want, and the tools are available to do it...but i think that your plan should be at least somewhat re-considered.

 

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.