Skip to content

API reference

cy.one — Core > Events. Cytoscape.js signatures, parameters and return contract.

cy.one — Core › Events

Preview · Core › Events

Read in the complete API reference · All API members

#cy.one(events, callback)

// Bind a direct listener
cy.one(events: string, callback: EventHandler<NodeData, EdgeData>): this

Like on(), but the handler runs at most once and then removes itself.

Parameters
NameDescription
events: string

one or more space-separated event names

callback: EventHandler<NodeData, EdgeData>

the handler, when delegating

Returns this

this core, for chaining

Example

let count = 0;
cy.one('example', () => count++);
cy.emit('example');
cy.emit('example');
console.log(count); // 1

Example graph and handle setup

#cy.one(events, predicate, callback)

// Bind a delegated listener
cy.one(events: string, predicate: ElePredicate<NodeData, EdgeData>, callback: EventHandler<NodeData, EdgeData>): this

Like on() with delegation, but the handler runs at most once.

Parameters
NameDescription
events: string

one or more space-separated event names

predicate: ElePredicate<NodeData, EdgeData>

( ele) => boolean over the event target

callback: EventHandler<NodeData, EdgeData>

the handler

Returns this

this core, for chaining

Example

const seen = [];
const handler = event => seen.push(event.target.id());
cy.one('example', node => node.isNode(), handler);
cy.getElementById('a').emit('example');
console.log(seen); // ['a']

Example graph and handle setup

Also spelled cy.once.