I have a Php
script that counts how many times a file is downloaded using Php
and JQuery
. I have a function getStatus()
that calls a file Php
every second and consumes a lot of bandwith. I tried then to call getStatus()
only when the user clicks on download link, but even if I get no error, is not working as wanted, the counter (javascript
part) is not updated in real time. Only putting alert()
after getStatus()
makes it working.
index.php
<?php include($_SERVER["DOCUMENT_ROOT"] . "/php/static_download.php"); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Download Page</title>
<script src="jsc/jquery.min.js"></script>
<script src="jsc/download.js"></script>
<link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
<div class="mainbox">
<div class="box1">
<span class="title">Download</span>
</div>
<div class="cleardiv"></div>
<div class="sbar"></div>
<div class="cleardiv"></div>
<div class="box2">
<span class="row">File: <a href="files/exampleA.zip" class="link">exampleA.zip</a> downloaded <span id="item1"><?php echo $item['item1']; ?></span> times</span><br />
<span class="row">File: <a href="files/exampleB.zip" class="link">exampleB.zip</a> downloaded <span id="item2"><?php echo $item['item2']; ?></span> times</span><br />
<span class="row">File: <a href="test/test.zip" class="link">test.zip</a> this file will not be counted</span><br />
</div>
<div class="cleardiv"></div>
</div>
</body>
</html>
call_download.php
<?php
$item['item1'] = get_download_count('exampleA.zip');
$item['item2'] = get_download_count('exampleB.zip');
if (!headers_sent()) {
header('Content-type: application/json; charset=utf-8');
}
echo json_encode($item);
?>
download.js
$(function() {
getStatus();
});
function getStatus() {
$.getJSON('/php/call_download.php', function(data) {
$('#item1').html(data.item1);
$('#item2').html(data.item2);
});
setTimeout("getStatus()",1000);
}
As you can see getStatus()
function is called every one second and so I tried to modify download.js as follow:
download.js
$(document).ready(function() {
$(document).on('click','.link',function(event) {
getStatus();
alert("Thanks for downloading my files!");
});
});
function getStatus() {
$.getJSON('/php/call_download.php', function(data) {
$('#item1').html(data.item1);
$('#item2').html(data.item2);
});
}
For some reason that I cannot understand, appending alert()
after getStatus()
makes the counter work... if I comment alert()
stops working.
PS: I tested getStatus() and with my surprise I noted a strange thing...
$(document).ready(function() {
$(document).on('click','.link',function(event) {
getStatus();
alert("Thanks for downloading my files!");
});
});
function getStatus() {
$.getJSON('/php/call_download.php', function(data) {
$('#item1').html(data.item1);
$('#item2').html(data.item2);
alert("success");
}).done(function(getStatus) {
alert("success");
}).fail(function(getStatus) {
alert("error");
}).always(function(getStatus) {
alert("complete");
});
}
with alert("Thanks for downloading my files!");
returns success
, commenting //alert("Thanks for downloading my files!");
returns error
... works only with alert()
.... the counter is not updated in real time for me...
Considerations
As you can see getStatus()
function is now called only when user clicks on link thanks to setTimeout()
function added inside on click event. Now works as expected.
download.js
$(document).ready(function() {
$.ajaxSetup({cache: false});
// $.ajaxSetup({async: false});
$(document).on('click','.link',function(event) {
setTimeout("getStatus()",1000);
});
});
function getStatus() {
$.getJSON('/test/php/call_download.php', function(data) {
$('#item1').html(data.item1);
$('#item2').html(data.item2);
});
}
Thanks for the time spent to help me!