Chrome扩展程序,javascript:这是为什么发射两次? [英] Chrome extension, javascript: Why is this firing twice?

查看:68
本文介绍了Chrome扩展程序,javascript:这是为什么发射两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  function test()$ b在我的(测试)Chrome扩展中有一个非常简单的代码: $ b {
alert(In test!);


chrome.tabs.onUpdated.addListener(函数(tabid,changeinfo,tab){
var url = tab.url;
if(url!= =未定义){

test();
}
});

我的问题是,为什么 test()发射两次?而且更重要的是,我该如何让它燃烧一次? 看看事件发生时不同的状态被派遣。我认为,当国家加载或国家完成时,它会被派遣一次。如果是这样的话,那么你的问题将被解决:

  function test()
{
警报(测试中!);


chrome.tabs.onUpdated.addListener(函数(tabid,changeinfo,tab){
var url = tab.url;
if(url!= = undefined&&; changeinfo.status ==complete){

test();
}
});


I have a very very simple bit of code in my (test) Chrome extension:

    function test()
    {
    alert("In test!");
    }

   chrome.tabs.onUpdated.addListener(function(tabid, changeinfo, tab) {
    var url = tab.url;
        if (url !== undefined) {

        test();
    }
   });

My question is, why is test() firing twice? And more importantly, how do I make it fire just once?

解决方案

Have a look at what the different states are when the event is dispatched. I presume, that it is getting dispatched once when the state is "loading" or when the state is "complete". If that is the case, then your problem would be fixed with:

 function test()
    {
    alert("In test!");
    }

   chrome.tabs.onUpdated.addListener(function(tabid, changeinfo, tab) {
    var url = tab.url;
        if (url !== undefined && changeinfo.status == "complete") {

        test();
    }
   });

这篇关于Chrome扩展程序,javascript:这是为什么发射两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆