preventing event propagation

The following results in an infinite loop. Proceed and Undo keep getting called one after the other once I press a button on the form.
What am I missing.
Does the event need to be removed from a queue to get something like this to work?

...
  myForm = dhxTabbar.cells("a1").attachForm(formData);
  currentEventId = myForm.attachEvent("onButtonClick", Proceed);

...
function Proceed() {
  console.log("I am proceeding");
  myForm.hideItem("name2");
  myForm.showItem("name3");
  myForm.detachEvent(currentEventId);
  currentEventId = myForm.attachEvent("onButtonClick", Undo);
}

function Undo() {
  console.log("I am undoing");
  myForm.hideItem("name3");
  myForm.showItem("name2");
  myForm.detachEvent(currentEventId);
  currentEventId = myForm.attachEvent("onButtonClick", Proceed);
}

Hi

inside form there is an array with handlers, when form callEvent it loop throu all items, and you have:
var events = [];
callEvent = function(){
for (var i=0; i<events.length; i++) {
events[i].apply(this,argsā€¦); // here you detach one attach another, events.length increased, just attached event called.

you need to attach another event ising timenout, in separate thread:

myForm = new dhtmlXForm("myForm", formData); evId = myForm.attachEvent("onButtonClick", proceed); //... var evId; function proceed() { console.log("proceed") myForm.detachEvent(evId); window.setTimeout(function(){ evId = myForm.attachEvent("onButtonClick", undo); },1); } function undo() { console.log("undo") myForm.detachEvent(evId); window.setTimeout(function(){ evId = myForm.attachEvent("onButtonClick", proceed); },1); }

Thanks Andrei