Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-1636] Two switches cause repeated event firing

GitHub Issuen/a
TypeBug
PriorityTrivial
StatusClosed
ResolutionInvalid
Resolution Date2011-04-15T02:58:00.000+0000
Affected Version/sn/a
Fix Version/sRelease 1.5.0
ComponentsiOS
Labelsdefect, ios, iphone, switch
Reporterctredway
AssigneeBlain Hamon
Created2011-04-15T02:57:59.000+0000
Updated2017-03-02T19:05:18.000+0000

Description

A customer reported that they were trying to have 2 switches be linked to each other so that when one value changed the other switch changed. The below code, when run, causes many many events to fire when the window is opened. This was run on the iphone, sdk 4.0.2 & 1.4.1

var win = Titanium.UI.currentWindow;

var sw1= Titanium.UI.createSwitch({ value:false, left:215,top:100});
win.add(sw1);

var sw2= Titanium.UI.createSwitch({ value:true, left:215,top:200});
win.add(sw2);

sw1.addEventListener('change',function(e){

sw2.value = !sw2.value;

});

sw2.addEventListener('change',function(e){

sw1.value = !sw1.value;

});

Comments

  1. Stephen Tramer 2011-04-15

    The problem is that this causes an infinite loop, REGARDLESS of when the event is triggered. Switch value 'change' events are fired whenever the value is set, and this includes creating a switch with a 'value' property.

    Every time a switch was flicked for this window, the following would happen, regardless of when:

    s1 -> s2 -> s1 -> s2 -> s1 ->...

    The customer needs additional logic in their code to ensure that no switch is flipped twice by this requirement.

  2. ctredway 2011-04-15

    This happens when just loading the window, without touching a switch. So you are saying that just by having 2 switches with listeners to each other, and not touching either one, this will happen?

  3. Stephen Tramer 2011-04-15

    I am saying two things.

    • Setting 'value' on a control at any time causes the change event to be fired. There are certain circumstances where this might actually be a bug.
    • The customer's logic is wrong to begin with, and even if the event was not fired at window load, flipping a switch would produce exactly the same result.
  4. ctredway 2011-04-15

    I can see how the listeners would cause an infinite loop, but when a control is created and a value is set, IMO, should not fire the change event. This is backwards from what most expect.

  5. Lee Morris 2017-03-02

    Closing as invalid.

JSON Source