简体   繁体   English

添加回调函数后,JavaScript停止工作

[英]JavaScript stops working when a call back function is added

I am experiencing a weird problem in my web page. 我的网页出现奇怪的问题。 JavaScript attached to a href gets called just fine and stops working if the called javascript function adds a statement with call-back function in it. 附加到href的JavaScript可以正常调用,并且如果被调用的javascript函数在其中添加了带有回调函数的语句,则该JavaScript可以停止工作。 Not even the alert placed as the very first statement in the JS func called from href fires in that case. 在这种情况下,甚至不会将警报作为从href调用的JS函数中的第一条语句触发。

Here is an example: Following works just fine. 这是一个示例:跟踪效果很好。

when clicking on this link 当点击此链接时

<a href = "#" onclick="getMyPosition(); return false;">refresh</a>,

following js func gets called just fine and I see both alert messages indicating that getMyPosition() was executed. 以下js func被调用就好了,我看到两条警报消息都表明已执行getMyPosition()。 Note: at this time (ie when the js is seemingly working) the call-back function ("handleposition") is NOT defined but it does not raise any errors! 注:在这个时候(即当JS貌似是工作)的回调函数(“handleposition”) 没有定义,但它不会引发任何错误! weird in its own right :) 怪异的:)

function getMyPosition() { 
  alert("it worked 1.");
  if ( navigator.geolocation )
  {
    alert("it worked 2.");
    navigator.geolocation.getCurrentPosition(handlePosition, handleError, {
    enabledHighAccuracy: true,
    maximumAge: 60000,
    timeout:    30000
    });
  }
}

Now, if I add the following function in the script block, all stops working and it appears that the refresh link was even clicked on. 现在,如果我在脚本块中添加以下功能,所有功能将停止工作,并且似乎甚至单击了刷新链接。 Since everything else on the webpage (which contains JavaScript events etc attached to other items) keeps working fine, my assumption is that JavaSctipt on the page is working fine. 由于网页上的所有其他内容(包含附加到其他项目的JavaScript事件等)均正常运行,因此我认为页面上的JavaSctipt正常运行。 Just this link stops calling the func defined on onClick event. 只是此链接停止调用onClick事件上定义的func。

adding the following call-back function causes the behavior as if "refresh" link wasn't even clicked on: 添加以下回调函数会导致该行为,就好像没有单击“刷新”链接一样:

function handlePosition(pos)
{
  alert(pos.coords.latitude);
  alert(pos.coords.longitude);
  window.location.href = "/map?js=y&amp;lat=pos.coords.latitude&amp;lon=pos.coords.longitude";
}

Now if remove this function, the href link shown above will start working again and getMyPosition will get called and I see alert messages again! 现在,如果删除此功能,上面显示的href链接将再次开始工作,并且将调用getMyPosition,并且我会再次看到警报消息! I do not see any script errors on the page Both IE 11 and Chrome as exhibiting this behavior. 我在IE 11和Chrome页面上都没有看到任何脚本错误,都表现出这种现象。 I am on Windows 7 32bit machine. 我在Windows 7 32位计算机上。

window.location.href = "/map?js=y&amp;lat=pos.coords.latitude&amp;lon=pos.coords.longitude";

应该可能是:

window.location.href = "/map?js=y&lat=" + pos.coords.latitude + "&lon=" + pos.coords.longitude;

Possibly the biggest problem was the missing error handler for the geolocation callback. 可能最大的问题是缺少地理定位回调的错误处理程序。 Bringing up Chrome's debugger via F12 quickly revealed the problem. 通过F12调出Chrome的调试器很快发现了问题。

Adding the error handler seemed to fix the problem, but on the way there I cleaned-up a few other things. 添加错误处理程序似乎可以解决问题,但是在此过程中,我清理了一些其他内容。 Here's a fiddle showing it working 这是一个小提琴,它正在工作

Here's how I'd code the link to work in all browsers: 这是我对链接进行编码以使其在所有浏览器中都能正常工作的方式:

<a href="#" onclick="return(getMyPosition())">refresh</a>

And here's the revised JS code for handle position and the new handleError routine. 这是修改后的JS代码(用于句柄位置)和新的handleError例程。

Note the "return false" added to your main function: 注意添加到您的主要功能的“ return false”:

function getMyPosition() {
    alert("it worked 1.");
    if (navigator.geolocation) {
        alert("it worked 2.");
        navigator.geolocation.getCurrentPosition(handlePosition, handleError, {
            enabledHighAccuracy: true,
            maximumAge: 60000,
            timeout: 30000
        });
    }
    return false;
}

function handlePosition(pos) {
    alert(pos.coords.latitude);
    alert(pos.coords.longitude);
    window.location.href = "/map?js=y&amp;lat=" + pos.coords.latitude + "&amp;lon=" + pos.coords.longitude;
}

function handleError(error) {
    alert(error);
}

Put return false; 放置return false; in at the end of the getMyPosition function 在getMyPosition函数的末尾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM