【发布时间】:2018-07-12 20:16:16
【问题描述】:
我有来自 html5 的地理位置代码。在此代码中,当我单击按钮时,当我单击“试用”按钮时,它将显示纬度和经度数据。 演示:demo 我已经自定义了代码并使用 ajax 将数据插入到数据库中。现在我想按下按钮,我想在 5 秒后自动发送数据。
<script
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<button onclick="getLocation()">Try It</button>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
var lat = position.coords.latitude;
var long = position.coords.longitude;
//AJAX CALL THIS WILL PASS THE VALUE OF JAVASCRIPT TO PHP
$.ajax({
type: 'post',
url: 'ajax-processor.php',
data: {
lat: lat,
long: long
},
success: function(html) {
alert("Success");
}
});
}
</script>
要插入我的 ajax 数据,我有这段代码,它直接插入到数据库 (ajax-processor.php)
$lat = $_POST['lat'];
$long = $_POST['long'];
//your code to insert to the database here using the $lat and $long variable
现在我想在没有任何按钮的情况下在 5 秒后插入此数据。在地理位置代码中我有这个代码
<button onclick="getLocation()">Try It</button>
我想隐藏这个按钮,经纬度数据会在5秒后自动发送到数据库。 总之,我想插入我的纬度和经度数据将在 5 秒后发送到数据库。 请帮帮我。
【问题讨论】:
-
试试这个代码:
setTimeout(function() { getLocation();}, 5000); -
或更短的
setTimeout(getLocation(), 5000);
标签: php ajax database html geolocation