Welcome Guest! To access all forums & features, please register an account or sign-in. → Why register?



AJAX every min


3 replies to this topic - - - - -

#1 Craig Hopson

    Neowinian

  • 60 posts
  • Joined: 17-November 12
  • Location: London
  • OS: Windows 8 :-(

Posted 19 November 2012 - 17:48

hi guys i have this code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML GeoLocation Test</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
var watchID=0;

function startLocationTracking(){
	if (navigator.geolocation) {
		watchID = navigator.geolocation.watchPosition(showCurrentLocation,errorHandler,{enableHighAccuracy: true});
	} else {
	  
	}
}

function showCurrentLocation(position){
var currentTime = new Date(position.timestamp)
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes < 10){
minutes = "0" + minutes
}
var seconds = currentTime.getSeconds()
if (seconds < 10){
seconds = "0" + seconds
}
var tim = hours + ":" + minutes + ":" + seconds;
document.getElementById("mylocation").innerHTML = "Current Latitude : " + position.coords.latitude + "<br /> Longitude : " + position.coords.longitude + "<br> Time: " + tim;

var data = { 'latitude': position.coords.latitude , 'longitude': position.coords.longitude, 'time': position.timestamp };
$.ajax({
	type: 'POST',
	url: 'post.php',
	data: data
});

}

function errorHandler(error){
	  alert("Error while retrieving current position. Error code: " + error.code + ",Message: " + error.message);
}

function logit(){
  }

</script>
</head>
<body onload="startLocationTracking()">
<div id="main">
	<div id="mylocation"></div>
</div>
</body>
</html>
the problem i have is that it posts (stores in database) every time the location cahanges which is good but it does it every second how can i ONLY log it every min

Thanks


#2 +Seahorsepip

    http://seapip.com

  • 939 posts
  • Joined: 23-January 11
  • Location: Vlissingen, Netherlands
  • OS: Windows 8 Pro

Posted 19 November 2012 - 20:26

The part that pushes it to your database is:
$.ajax({
        type: 'POST',
        url: 'post.php',
        data: data
});

changing it into:
function everymin() {
$.ajax({
        type: 'POST',
        url: 'post.php',
        data: data
});
}
setInterval( "everymin()", 1*60*1000 );

Should make it only store once a minute in the database.

#3 OP Craig Hopson

    Neowinian

  • 60 posts
  • Joined: 17-November 12
  • Location: London
  • OS: Windows 8 :-(

Posted 19 November 2012 - 21:20

Thank you for your quick resonce will it still carrie the variables from one function to another?

#4 +Seahorsepip

    http://seapip.com

  • 939 posts
  • Joined: 23-January 11
  • Location: Vlissingen, Netherlands
  • OS: Windows 8 Pro

Posted 19 November 2012 - 21:32

View PostCraig Hopson, on 19 November 2012 - 21:20, said:

Thank you for your quick resonce will it still carrie the variables from one function to another?
It will do exactly the same as before it only stores every 60sec now ^^