Javascript 在第一个函数完成时触发下一个函数
我有以下javascript地理标记脚本:Javascript 在第一个函数完成时触发下一个函数,javascript,function,triggers,callback,Javascript,Function,Triggers,Callback,我有以下javascript地理标记脚本: window.onload = function(){ var x=document.getElementById("output"); getLocation(); function getLocation() { if (navigator.geolocation) { navigator.geolocation.watchPosition(reverseGeoLookup); } else { x.innerHTML="Geoloca
window.onload = function(){
var x=document.getElementById("output");
getLocation();
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.watchPosition(reverseGeoLookup);
}
else
{
x.innerHTML="Geolocation is not supported by this browser.";
}
}
function reverseGeoLookup(position) {
console.log(position);
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var req = new XMLHttpRequest()
req.open("GET", "http://maps.googleapis.com/maps/api/geocode /json?latlng="+lat+","+lon+"&sensor=true", true)
req.onreadystatechange = function() {
if(req.readyState == 4) {
var result = JSON.parse(req.response).results
for(var i = 0, length = result.length; i < length; i++) {
for(var j = 0; j < result[i].address_components.length; j++) {
var component = result[i].address_components[j]
//console.log(component.long_name);
if(~component.types.indexOf("postal_code")) {
var out = document.getElementById('output');
out.value = component.long_name;
return false;
}
}
}
}
}
req.send()
}
setTimeout(function(){
var str = document.getElementById('output').value ;
var res = str.substring(0,3);
document.getElementById("demo").innerHTML=res;
},1000);
setTimeout(function(){
window.location = 'mylocationurl' + document.getElementById('demo').innerHTML + '/' + 'index.html'
},7000);
}
window.onload=function(){
var x=document.getElementById(“输出”);
getLocation();
函数getLocation()
{
if(导航器.地理位置)
{
导航器。地理位置。观察位置(反向定位);
}
其他的
{
x、 innerHTML=“此浏览器不支持地理位置。”;
}
}
功能反转向上(位置){
控制台日志(位置);
var lat=位置坐标纬度;
var lon=位置坐标经度;
var req=new XMLHttpRequest()
请求打开(“获取”http://maps.googleapis.com/maps/api/geocode /json?latlng=“+lat+”,“+lon+”&传感器=真”,真)
req.onreadystatechange=函数(){
如果(req.readyState==4){
var result=JSON.parse(req.response).results
对于(变量i=0,长度=result.length;i
代码获取用户位置,将其转换为英国邮政编码,然后将用户重定向到相应的页面。目前,该网站正在使用setTimeout
通知浏览器何时重定向。我想更改此设置,以使事件按顺序发生,即查找位置,然后在完成时缩短为前3位数字,然后在完成重定向后再对访问者进行重定向。理想情况下,我不希望使用jQuery。
有人能帮我吗
提前谢谢 您可以使用回调功能成功完成此操作
function getLocation(param, callback) {
getLocation stuff
callback();
}
对不起。。。只是现在我想知道你需要什么 所以。。。您需要在“getLocation”功能结束时启动的回调函数吗 如果在XMLHttpRequest完全加载时调用回调函数,则需要在XMLHttpRequest对象的onreadystatechange属性中插入回调函数。 例如:
function getLocation(){
if (navigator.geolocation){
navigator.geolocation.watchPosition(reverseGeoLookup(position,function(longName){
console.log("function called, scope:",this,"args:",arguments);
//callback function here
//you can insert inside the callback the script inside "setTimeout"
var res = longName.substring(0,3);
document.getElementById("demo").innerHTML=res;
//and after 7 sec. from the ajax request complete change page:
setTimeout(function(){
window.location = 'mylocationurl' + document.getElementById('demo').innerHTML + '/' + 'index.html'
},7000);
}));
}else{
x.innerHTML="Geolocation is not supported by this browser.";
}
window.onload = function(){
var x=document.getElementById("output");
getLocation();
}
function reverseGeoLookup(position,callback) {
console.log(position);
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var req = new XMLHttpRequest()
req.open("GET", "http://maps.googleapis.com/maps/api/geocode /json?latlng="+lat+","+lon+"&sensor=true", true)
req.onreadystatechange = function() {
if(req.readyState == 4) {
var result = JSON.parse(req.response).results
for(var i = 0, length = result.length; i < length; i++) {
for(var j = 0; j < result[i].address_components.length; j++) {
var component = result[i].address_components[j]
//console.log(component.long_name);
if(~component.types.indexOf("postal_code")) {
var out = document.getElementById('output');
out.value = component.long_name;
//insert here the callback if you need to wait fully request load
//note the argument inside of callback become longName.
callback(component.long_name);
return false;
}
}
}
}
}
req.send();
}
}
函数getLocation(){
if(导航器.地理位置){
navigator.geolocation.watchPosition(reverseGeoLookup)(位置,函数(longName){
log(“调用的函数,作用域:”,this,“args:”,参数);
//这里的回调函数
//您可以在回调中插入“setTimeout”中的脚本
var res=longName.substring(0,3);
document.getElementById(“demo”).innerHTML=res;
//在ajax请求完成更改页面7秒后:
setTimeout(函数(){
window.location='mylocationurl'+document.getElementById('demo').innerHTML++'/'+'index.html'
},7000);
}));
}否则{
x、 innerHTML=“此浏览器不支持地理位置。”;
}
window.onload=函数(){
var x=document.getElementById(“输出”);
getLocation();
}
函数reverseGeoLookup(位置,回调){
控制台日志(位置);
var lat=位置坐标纬度;
var lon=位置坐标经度;
var req=new XMLHttpRequest()
请求打开(“获取”http://maps.googleapis.com/maps/api/geocode /json?latlng=“+lat+”,“+lon+”&传感器=真”,真)
req.onreadystatechange=函数(){
如果(req.readyState==4){
var result=JSON.parse(req.response).results
对于(变量i=0,长度=result.length;i我希望它能有所帮助这真的是你代码的复制品吗?您在这里有一个语法错误
window.location=mylocationurl'+文档。
mylocationurl'是我在发布之前删除实际url的地方。所有代码都起作用,因为它应该首先删除mylocationurl
之后的撇号…正如我之前所说,代码的功能是正确的-我只是想改变它来触发一个接一个的函数,有人能帮我吗?我想这正是我所需要的,但是我对这种编码非常陌生,我不知道如何实现它-你能帮我进一步吗?非常感谢你抽出时间来帮助我!