weixin_33691817 2018-04-04 12:06 采纳率: 0%
浏览 42

JS重新创建事件监听器

I'm working with JQuery and ajax.

SCENARIO: The scenario is the following. I have a table with different features and each row has it's own button to call "show data " function. With the first call I check some microservices that manipulated the origin data. If data is available ,I show a button for every manipulated data to show in a chart. Every Button gets a listener created in de success method of an ajax call. So there can be three scenarios:

First: show data is called the first time. So there is no Eventlistener created yet. In that case I create I new one for each button.

Second: Imagine I clicked a button to show the data in a chart. After that, the chart gets closed. Now I want to show the same data again. So my Eventlistener should stay the same and there should not be another one added.

Third: I click another feature in the table which might have data the is manipulated differently or will be shown in another chart (e.g. pie chart insted of flow chart.) So the scenario is pretty much like in case one but now there is an old Listener to delete.

As a summary: I have to save the Eventlistener Object persistend when it gets createt because when case 3 turns up, I want to call removeEventlistener but I get an error that this object is not defined:

I'm a total noob in js. This scenario results in the problem, that the eventarguments are not updated and you can see that the data output of listeners pops up for a second and gets overlayed by newer. And when you do a few attemps, it ends in chaos where nothing fits. By this I mean the wrong data is shown in a wrong chart (one of an older event.). So I wanted to delete the old listener and recreate it. Is this a possible solution or is there a better way to do it?

The whole funtion looks like this:

function showData(id,category,feature,date,caller,already_called){

    document.getElementById("originButtonDiv").style.visibility='visible';
    document.getElementById("squareButtonDiv").style.visibility='hidden';




    try{
        //show data again -> maintain listener
        if(old_already_called == 0 && already_called == 1){
            old_already_called = already_called;
        }
        //New feature choosen -> Delete old listener and create new one
        if(old_already_called == 1 && already_called == 0){
            $.ajax({
                  type:'GET',
                  url:"${path}/user/...
                  dataType:'text',
                  async: false,
                  success:function genTable(data){      

                            var originDataButton = document.getElementById("originDataButton");
                                //originDataButton.removeEventListener("click",_listener,true);
                                originDataButton.addEventListener("click", function _listener(){
                                originDataButtonClicked(id,category,feature,date);                                                              
                                }, true);



                  if(data.localeCompare("true")==1){                
                              document.getElementById("squareButtonDiv").style.visibility='visible';    
                                var squaredDataButton = document.getElementById("squaredDataButton");
                                    //squaredDataButton.removeEventListener("new_click",_squared_listener,true);                                                
                                    squaredDataButton.addEventListener("click",function _squared_listener(){
                                    squaredDataButtonClicked(id,category,feature,date);                     
                                    }, true);   
                        }
                  }
            });
            old_already_called = 0;
        }
    }
    catch{
        //first call --> create new Eventlistener
        $.ajax({
          type:'GET',
          url:"${path}/user/...
          dataType:'text',
          async: false,
          success:function genTable(data){      

                    var originDataButton = document.getElementById("originDataButton");
                        originlistener = originDataButton.addEventListener("click", function _listener(){
                        originDataButtonClicked(id,category,feature,date);  
                    }, true);





                  if(data.localeCompare("true")==1){                
                      document.getElementById("squareButtonDiv").style.visibility='visible';    
                        var squaredDataButton = document.getElementById("squaredDataButton");                                           
                            squaredDataButton.addEventListener("click",function _squared_listener(){
                            squaredDataButtonClicked(id,category,feature,date);                     
                            }, true);   
                }
          }
        });

        old_already_called = 1;
    }

}

Update: I'm pretty sure now that the problem is the usage of ajax. I defined a logic tree for my use case because I have to detect whether an Eventlistener should be created, persist unchanged or deleted and recreated. I guess I can differentiate these circumstances in my code but the problem is, that I define the listener in the ajax success method. Between two calls the listener is undefined. So is there a possibility to save a listener object peristent on the client between to ajax calls so that I can delete it within future calls? I guess this would be the solution for my problem.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 VMware 云桌面水印如何添加
    • ¥15 用ns3仿真出5G核心网网元
    • ¥15 matlab答疑 关于海上风电的爬坡事件检测
    • ¥88 python部署量化回测异常问题
    • ¥30 酬劳2w元求合作写文章
    • ¥15 在现有系统基础上增加功能
    • ¥15 远程桌面文档内容复制粘贴,格式会变化
    • ¥15 这种微信登录授权 谁可以做啊
    • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
    • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”