JS script, How to listen Add event only once.

Nicholasfox shared this question 7 months ago
Answered

When I create an object, I want to automaticlly get an object that is symmetrical to the created one about the Y-axis. But I get caught in an endless loop when using js script as below.

How to listen Add event only once. Please help, thanks.


function ggbOnInit() {

ggbApplet.registerAddListener("AutoSym")

}

function AutoSym(obj) {

ggbApplet.evalCommand("Reflect("+obj+", yAxis)")

}

https://www.geogebra.org/ca...

Best Answer
photo

Problem 1

All (?) processes in GGB run asynchronous/parallel. That means: if a new object is created in an AddListener function, this happens immediately and parallel to the actual running AddListener function with an additional concurrent AddListener function (instance)

Solution: unregister the add listener before creating the new object.

.


Problem 2

If only one object is created, then the AddListener can be registered again immediately after the creation of the (mirrored) new object. But if more objects are created (e.g. sides of a polygon) then the re-activation of the listener must be delayed. To my knowledge there is no synchronization command for this. As a workaround I would create a timeout.

.

Problem 3

Not all objects can be mirrored. The corresponding object types are not allowed to be handled in the AddListener function.

Comments (2)

photo
2

Problem 1

All (?) processes in GGB run asynchronous/parallel. That means: if a new object is created in an AddListener function, this happens immediately and parallel to the actual running AddListener function with an additional concurrent AddListener function (instance)

Solution: unregister the add listener before creating the new object.

.


Problem 2

If only one object is created, then the AddListener can be registered again immediately after the creation of the (mirrored) new object. But if more objects are created (e.g. sides of a polygon) then the re-activation of the listener must be delayed. To my knowledge there is no synchronization command for this. As a workaround I would create a timeout.

.

Problem 3

Not all objects can be mirrored. The corresponding object types are not allowed to be handled in the AddListener function.

photo
2

Problem 4

Double, or more the one, addlistners makes no sense in my eyes?

Problem 5

all new created objects become reflected, is this really intended?

perhaps selection by ggbApplet.getObjectType(o)

© 2021 International GeoGebra Institute