Search code examples
javascriptlistenerdom-events

Change event listener on


I have some variable let newBl = someFunction which subscribing on some data coming, and I have declaring of some listener newBl.on('data',(data)=>{}). I can remove this listener like newBl.off('data'), but I need redeclare this this listener. Is there are some method for this like newBl.change() or something like that?


Solution

  • You could create a separated function so you could reuse it withour repeating yourself like :

    newBl.off('data').on('data',(data)=>{
        common_function(data);
    });
    
    function common_function(data){
        //Your logic
    }