jquery auto refresh script problem - php

i wrote myself a block of jquery codes to auto refresh a div
just want it to reload every 10 seconds . but problem is after the time i specified in my code script going crazy reload every second
<script>
var auto_refresh = setInterval(function(){
$(\'#showDIV\').slideUp(\'300\').load(\'movies.php\').slideDown(500);},10000);
</script>

I think you have to use setTimeout() and not setInterval()
read the difference between the two here.

Related

How to pass variable to php script from Jquery $(document).ready function

I am building a chat. I have this Jquery working code which calls logs.php every second and refreshes the chat.
$(document).ready(
function(e) {
$.ajaxSetup({cache:false});
setInterval(function() {
$('#chatlogs').load('logs.php');
updateScroll();
}, 1000);
}
);
As you can see, also updateScroll, a JS function on my page, gets called. Updatescroll creates a variable, which I would like to pass on to logs.php, is there any way to do this? In other words, updatescroll basically checks everysecond if the user has scrolled up to the top of the chat. If so, I am gonna tell logs.php to load -say - another 10 messages. But in order to do this, I have to have something that from updatescroll passes on to the Jquery function and thus onto logs.php. You get it? Thanks
First, when it comes to ajax, I would recommend using a window.setTimeout, intervals can get tricky when you are running things asynchronously (if one call hangs you can end up with multiple calls to the same script).
so something more like:
(function($){
var update_messages = function(){
var count = updateScroll();
$('#chatlogs').load('logs.php?count='+count, function(){
window.setTimeout(update_messages, 1000);
});
}
$(document).ready(function(){
$.ajaxSetup({cache:false});
update_messages();
});
})(jQuery);
Then in your PHP script the "count" would be available via $_GET['count'].
EDIT: you can see an anonymous function is being sent as a second argument to load, this will be called AFTER the AJAX call is complete, so we can make sure only 1 of these is running at a time

Refresh div on same page with external fetched data

I am stuck again with a problem, let me explain it to you.
Inside the div I have fetched data with HTML SIMPLE DOM from other site. Like
<div id="data">.....</div>
It will refresh each and every time user will refresh the page. But I want something extra. What I wanna do is, refresh the div (inside which external data is fetched and added) periodically after 5 seconds.
Both the PHP SIMPLE HTML DOM script and this div is on same page.
Now I only need, any jquery or javascript code to refresh the div with data id after each 5 seconds with new data fron other site and all this without refreshing the whole page.
UPDATE:
I have used this code
$(document).ready( function() {
function getTheTime(){
$.get('http://your-domain/file.php',function(data,status){
$('#data').html(data);
});
}
var refresh = setInterval(
"getTheTime()",
5000
);
});
But the problem is very very strange, why it is not refreshing the div? Infact I have set alert for the interval but it also didn't worked. What the real problem is? Why it is not getting data from file.php and why actually it is not refreshing the div??
I am using latest jquery CDN. http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js
$(function() {
setInterval(function(){
$('#data').load('site.php');
}, 5000);
});
Definitely a job for AJAX...
Since you say you're already using jQuery, I'll walk you through the steps quickly to get an AJAX function set up and run it on an interval.
Step 1: Create a PHP file which gets the data you want to put in the DIV...
Just make a PHP file and put the code in to get the data:
<?php echo "The time is " . date('Y-m-d H:i:s');
Step 2: Set up an AJAX function to get the data from that file...
function getTheTime(){
$.get('http://yourdomain.com/ajax/getthetime.php',function(data,status){
$('#data').text(data);
});
}
(It would be possible to use the .load function instead, but it's far less flexible if you want to do anything with the data before putting it in the DIV).
Step 3: Call that function on an interval...
Next, we need to set up an interval to call the new function every 5 seconds.
$(function(){
var refresh = setInterval(
getTheTime(),
5000
);
});
Instead of using setInterval to call the function every 5 seconds, you can use simple long polling technique to refresh your div every 5 seconds. The problem with setInterval is that if the ajax request doesn't complete in specified time (5 secs here) there will be the chain of ajax requests.
function getTheTime(){
$.ajax({
type: "POST",
url: "http://your-domain/file.php",
success: function(response) {
$('#data').html(response); //update your div
},
complete: function(){
setTimeout(
getTheTime, /* Refresh time */
5000 /* ..after 5 seconds */
);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
//display you error message
},
timeout: 5000 //Timeout is necessary to prevent chaining of unsuccessful ajax request
});
}

Javascript - run a php file every X seconds

I would like to be able to record the amount of seconds a person is on my site in the simplest way possible. Don't need google analytics or any other 3rd party sources.
The php script would create a connection to mysql and update the relevant values,
I found some script online, but it doesn't seem to be working:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script>
<script>
$(document).ready(function()
{
var refreshId = setInterval(function()
$.load('timeonpage.php?wzx=<?php echo $t; ?>&ip=<?php echo $ip; ?>');
}, 5000);
);
</script>
Thanks for any help!
There is a syntax error, your missing an opening to the setInterval function, and the closing of the .ready method
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script>
<script>
$(document).ready(function(){
var refreshId = setInterval(function(){ //SYNTAX ERROR HERE
$.load('timeonpage.php?wzx=<?php echo $t; ?>&ip=<?php echo $ip; ?>');
}, 5000);
}); // SYNTAX ERROR HERE
</script>
Additionally, I would recommend using the $.get method, as .load in JQuery is typically meant for loading HTML into a particular container:
var refreshId = setInterval(function(){
$.get('timeonpage.php',{wzx:<?php echo $t?>,ip:<?php echo $ip?>});
},5000);
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script>
<script>
$(document).ready(function()
{
var refreshId = setInterval(function(){ //ANOTHER ERROR
$.load('timeonpage.php?wzx=<?php echo $t; ?>&ip=<?php echo $ip; ?>');
}, 5000);
}); //ERROR ON THIS LINE
</script>
Well you're missing a '}' in there on the closing of document ready. Try that, might be something so simple, report back if it's not and will have another look at it :)
Also: If you'd consider a 3rd party service, take a look at GoSquared, seriously amazing site there :)
Edit: Another error on setInterval line :)
With your current method, a user might be counted as online for all 86,400 seconds in a day if they just leave their browser window open.
Personally, my site is set up such that every time a user actually loads a page, their "last page loaded" time is updated. Then, a cron script runs every minute and checks to see which users have loaded a page in that last minute. Counting those up allows me to determine reasonably well how much time each user has spent online.
Additionally, this would cause WAY less load on your server - your solution would probably kill it if you had more than 50 or so users on your site.
it would probably be a lot more efficient onLoad to call an Ajax script that saves the page and any other data you want to the database and then returns a unique id which you store in a variable. On the onbeforeunload event you could then make another call to the database with the unique id. Just my two cents...

Ajax refreshing on itself with include php

Ok basically here is my problem and I can't find anything about it on this site or any site. I am using Ajax for refreshing a include php file and its keeps refreshing on itself.Basically the first refresh is fine 10 seconds then this happens the second refresh is 5 seconds while refreshing on itself, then the next about 2 seconds doing same thing, and goes down to the 1 second. I don't know what is wrong why its doing this.
This is the code for refreshing. I am not doing the rest of the code. I know this is where the problem is because it causes the refreshing itself.
<script>
$("#active").load("active.phps");
setInterval(function(){
$("#active").load("active.php");
}, 10000);
</script>
Anyone can help I would be grateful. This is to help tell what friends you have online, and with this it slows down performance on my site and on my computer.
I think that problem could be in time of ajax loading, method is probably called properly but there are delays from ajax loading. Maybe you shoul consider using setTimeout?
Simple example:
$(function() {
function loadPage() {
$("#active").load("active.phps");
setTimeOut(loadPage, 10000);
}
setTimeOut(loadPage, 10000);
$(function() {
add the added lines to the beginning and end of your script:
$(function() {
$("#active").load("active.phps");
setInterval(function(){
$("#active").load("active.php");
}, 10000);
});
this runs the scriptafter everything is finished loading. So it waits basically.

Jquery/Ajax Refresh without reload or load the page?

I have a report page which dynamically generate the record and saved in the database. From database i need to show the dynamically updated records in the page without reload or load the contents using setInterval time function.
see my question Here
and use setInterval function with it
var refreshId = setInterval(function()
{
$("#yourContainer").load('yourPage.php');
}, 5000);
offcourse Jquery is prereq for this... so download jQuery from here and include it in your page
<script href="yourlink to the file"></script>
this code http://pastie.org/937213 loads ajax.php to #live div every 3 seconds

Categories