0

Im wondering in the following code, how could I set the callback property "value" to null?

The code takes a bunch of ids (31, 32 ,33) and then uses a callback function to monitor changes to values of these ids. When I run the code again using the same IDs, is there a way to set the callback property to null?

obsids = new Array();

function list() {
  if (arguments.length) {
    leng = arguments.length;
    for (i = 0; i < leng; i++) {
      obsids[i] = new LiveAPI(callback, "id "+arguments[i]);
      obsids[i].property = "value";
    }
  }

  function callback(args) {
    outlet(0, args[0] + " " + args[1] + " " + this.id);
  }
}
Andy
  • 61,948
  • 13
  • 68
  • 95
Ke.
  • 2,484
  • 8
  • 40
  • 78
  • sure list is just a list of ids (31 32 33) the list function in my software turns this list into an array, which you can iterate over – Ke. Mar 21 '14 at 12:50
  • There is no `.callback` property in your code. Maybe you should show us the implementation of `LiveAPI`. Also, what do you try to achieve by "setting callback to null"? Btw, `leng` is indeliberately global. – Bergi Mar 21 '14 at 13:46
  • sorry its property value not callback, leng is defined as a var above in my script. i didnt think this question was liveapi specific. It seems that there is a callback and I need to somehow set .property = value to .property="" somewhere :/ maybe on next run of the script so I should create a if statement? – Ke. Mar 21 '14 at 16:04
  • Ah, I see now what you want – Bergi Mar 21 '14 at 17:07

1 Answers1

0

You will need a specific callback for each obsids[i], and you need to create them in a closure for not loosing i.

function list() {
  var obsids = [];
  for (var i=0, len = arguments.length; i < len; i++)
    obsids[i] = createApi("id "+arguments[i]);

  function createApi(id) {
    var obsid = new LiveAPI(function callback(args) {
      obsid.property = null;
      outlet(0, args[0] + " " + args[1] + " " + this.id);
    }, id);
    obsid.property = "value";
    return obsid;
  }
  return obsids;
}
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375