Hi Guys,
I'm trying to build an ajax/jquery function that will run every ten seconds, and update a value if the ajax/getJson returns a value greater than the initial declared value. This is for a page view counter I am building.
1. When the page first loads, a JS var 'hitCount' will be declared, and it will be set to the value retrieved at the time of request.
2. Every ten seconds, I want a script to run, that will make a getJson request, check and see if the JSON value is greater than the current 'hitCount' value, and if so, update the hitCount value.
3. If the hitCount value was updated, the script will then update the displayed variable to the current value.
I'm still getting used to writing in JS, so I'm not sure if my syntax is correct. I think I'm pretty close though. Can someone look over this and tell me what to correct?
I'm trying to build an ajax/jquery function that will run every ten seconds, and update a value if the ajax/getJson returns a value greater than the initial declared value. This is for a page view counter I am building.
1. When the page first loads, a JS var 'hitCount' will be declared, and it will be set to the value retrieved at the time of request.
2. Every ten seconds, I want a script to run, that will make a getJson request, check and see if the JSON value is greater than the current 'hitCount' value, and if so, update the hitCount value.
3. If the hitCount value was updated, the script will then update the displayed variable to the current value.
I'm still getting used to writing in JS, so I'm not sure if my syntax is correct. I think I'm pretty close though. Can someone look over this and tell me what to correct?
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script> var hitCount = 1 </script>
<script>
$(document).ready(function(){
setInterval("
$.getJSON("http://myurl.com/test.cfc?method=dnsHitCount&dnsID=108,function(hitCount){
if (hitCount.HitCount > hitCount){
var hitCount = hitCount.hitCount;
window.alert("hitCount has updated");
/*NOT SURE HOW TO MODIFY THE DIV'S OUTPUT TO REFLECT NEW VALUE */
});
});
",10000);
});
</script>
</head>
<body>
The current hit count is
<div id="dnsGraph">
<script language="javascript">
document.write (hitCount); //prints the value of x
</script>
</div>
</body>
</html>