Reputation: 668
I am using jQuery to call a function when a button is clicked. This function that is being called is in PHP and returns logging information. I have tried to use setInterval()
but it calls the function at whatever rate I choose and tends to return duplicate logging information and most of the time it freezes. Is there a way that I can make repeated calls to a PHP function from jQuery, but only call the function after the last call is finished and returned?
Here is the jQuery code that makes involves the setInterval()
setInterval(
function(){
$.getJSON("ajax.php?function=tail&pointer=" + ftell,
function(data){
var obj = [];
obj = $.parseJSON(data);
ftell = obj[0];
$("#tail").append(obj[1]);
});
}, 2000);
Here is the PHP function that it calls
function tail(){
$file = "/path/to/the/log/file.log";
$handle = fopen($file, "r");
clearstatcache();
$currentSize = filesize($file);
$offset = $_REQUEST['pointer'] - $currentSize;
if ($_REQUEST['pointer'] == 0) {
fseek($handle, -1024, SEEK_END);
} else {
fseek($handle, $_REQUEST['pointer'], SEEK_END);
}
while ($buffer = fgets($handle)) {
$log .= $buffer . "<br />";
}
fclose($handle);
$results = array('0' => $currentSize, '1' => str_replace('"', '\"', $log));
echo json_encode($results);
}
Upvotes: 1
Views: 2739
Reputation: 218762
You can set a global variable to hold the status of your jQuery ajax call.Intialize it to null. In your success handler of your jQuery ajax, set it to completed. Then before you call this piece of code in your set interval, check the value of the variable. If it is completed ,then continue executing your code to call your php page via jquery ajax.
var globalAjaxResponseStatus="";
var int=self.setInterval("CheckData()",1000);
function CheckData()
{
if(globalAjaxResponseStatus=="completed")
{
globalAjaxResponseStatus=""
$.ajax({
url: "checkdata.php",
success: function(data){
globalAjaxResponseStatus="completed"
//Do what you want with the response
}
});
}
}
Upvotes: 0
Reputation: 1931
Like zzzzBov mentioned, something like this.
setTimeout('my_function()', 500);
function my_function() {
// method in PHP
setTimeout('my_function()', 500);
}
Make sure my_function is at the highest scope, the first setTimeout can go anywhere when you're ready to start executing. This executes my_function every 0.5 seconds.
Upvotes: 0
Reputation: 38345
Sounds like what you want is an AJAX call that recursively calls itself upon successfully returning. A very simple example might look something like this:
function foo() {
$.ajax({
url: 'url',
data: {
// data
},
success: function(data) {
// do stuff with data
foo();
}
});
}
You'd obviously have to handle the initial call to that function, and if you didn't want it to run indefinitely introduce some sort of stopping case.
Upvotes: 1
Reputation: 179086
This will trigger another ajax event five seconds after the first one has finished. It wont matter whether it's succeeded or failed.
function checkForUpdates() {
var delay = 5000; //in milliseconds, so 5s
setTimeout(function () {
$.ajax(...).always(checkForUpdates);
}, delay);
}
Some concerns with writing code like this can be that it doesn't contain a mechanism for cancelling.
Upvotes: 2
Reputation: 1190
Use setTimeout in the callback function of the ajax call.
setTimeout only executes the script once and placing this in the callback function ensures that the ajax call has been completed before creating another countdown timer to the ajax call.
Upvotes: 1