简体   繁体   English

javascript函数调用计时。 实施此的最佳方法

[英]javascript function call timing. The best way of implementing this

I have The following in my main javascript file: 我的主要javascript文件中包含以下内容:

window.onbeforeunload = function () {
    firstFunction();
}

$("a").live("click", function() {
   secondFunction();
}

When user clicks on any link that unloads the page (ie navigate to another page), both of those functions get called; 当用户单击任何卸载页面的链接时(即导航到另一个页面),这两个函数都将被调用。 FirstFunction() gets called first, then secondFunction() gets called. 首先调用FirstFunction(),然后调用secondFunction()。 I want the secondFunction() to get called before the firstFunction(); 我希望secondFunction()在firstFunction()之前被调用;

I was able to achieve this by doing this: setTimeout('firstFunction(),100), but I am wondering if there is a better way of doing this? 我能够通过执行以下操作来实现这一点:setTimeout('firstFunction(),100),但我想知道是否有更好的方法来做到这一点?

Thanks. 谢谢。

我建议您使用mousedown事件而不是click。

Try this - give all external links a class="external" 试试这个-给所有外部链接一个class =“ external”

window.onbeforeunload = function () {
    firstFunction();
}
$(document).ready(function() {
  $("a").click(function() {
     if ($(this).hasClass("external")) secondFunction();
  });
});

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

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