0
Answered

AddListener with System.Time.Seconds

Wouter van der Post 6 years ago in Other updated by Vladimir Ovchinnikov (expert) 6 years ago 5

Hello,


I've been using the following code to have a function execute at a certain time (the user can select the time with the touchscreen). Every minute I check if the selected time is reached, and execute some logic if it is:


IR.SetGlobalListener(IR.EVENT_GLOBAL_TAG_CHANGE, function(name, value) {
  if (name == "System.Time.Minutes") {
    if (parseInt(IR.GetVariable("System.Time.Hour")) == selectedHour && parseInt(IR.GetVariable("System.Time.Minutes")) == selectedMinutes) {
      // call some function ....
}
  }
});

Now I want to dynamically add more of these timers that have different times selected by the user, but you can only call SetGlobalListener once. Can you tell me how I can do this with IR.AddListener? I've tried:

IR.AddListener(IR.EVENT_TAG_CHANGE, IR.GetVariable("System.Time.Minutes"), function(name, value) {
  // logic
});


but that is not being called.

Can you help me out?

Under review

Hello.

After IR.EVENT_TAG_CHANGE you must specify a driver, not a system token. Check the system token, put in the if ... else. To allow the user to set the desired time in the panel project, add the project token and compare its value with the system token.

Okay, too bad we can't use a system token with AddListener.


I think I'll solve it like so:


var _timerActions = [];

IR.SetGlobalListener(IR.EVENT_GLOBAL_TAG_CHANGE, function(name, value) {
  if (name == "System.Time.Minutes") {
    for(var i = 0; i < _timerActions.length; i++) {
      _timerActions[i](value);
    }
  }
});


When a user selects a time on the panel and clicks "start" a function will be added to the _timerActions array. In those functions I can then check against IR.GetVariable("System.Time.Hour") and IR.GetVariable("System.Time.Minutes").

Yes, you can.

Do you have any more questions on this topic?

No, you can close this topic. tnx!