Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cy.on('select') callback only once

Tags:

cytoscape.js

I have a createTable function that receives a collection of nodes (or an array of nodes), then I am able to draw a table.

I am switching to cytoscape.js now, and I really don't know how to have a listener to my select event properly.

Doing this:

cy.on('select', 'node', function(event){
        window["selectedNodes"] = cy.$('node:selected');
});

I do have all information I need to draw my table, but I cannot call my createTable function inside it because it will call my function several times (once per node selected). I've already tried to use cy.on and cy.once, but without success.

Here is my question:

How can I have a listener to my selection event, get ALL selected nodes and call (only once) my createTable function ?

I can also obtain all selected node using this:

cy.elements('node:selected', console.log("my CallBack function"));

But as it is outside an event listener (select / click) it doesn't work as I need.

Please, any help is appreciated.

Thank you.

like image 555
dmartinez Avatar asked May 17 '26 08:05

dmartinez


1 Answers

Debounce your callback so if multiple events come in one after another, then they are effectively batched:

 var timeout;
 cy.on('select', 'node', function(event){
   clearTimeout( timeout );
   timeout = setTimeout(function(){
     window["selectedNodes"] = cy.$('node:selected');

     // and so on...
   }, 100); // may have to adjust this val

 });
like image 127
maxkfranz Avatar answered May 19 '26 04:05

maxkfranz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!