Search code examples
javascriptreactive-programmingspark-ar-studio

Spark AR Why if-else if construction doesn't work?


I don't understand why if-else construction doesn't work.

const Diagnostics = require('Diagnostics');
const Reactive = require('Reactive');


var num = 5;
var firstCondition= false;
var secondCondition = false;

function func(){
    firstCondition = Reactive.ge(num, 10);
    secondCondition = Reactive.lt(num, 10);
    if(firstCondition){             //false
      num = 0;
    } else if(secondCondition){     //true
      num = 1;
    }
}
func();

Diagnostics.watch("num - ", num);
Diagnostics.watch("firstCondition ", firstCondition);
Diagnostics.watch("secondCondition ", secondCondition);

num shows 0:( What am I doing wrong? Is it specific of reactive programming?


Solution

  • There are two issues in your example:

    • signals can be confusing and return true just because the variable is there, so you need to check: if (firstCondition.pinLastValue()) to get the value of the boolean signal.
    • secondly, I guess your function is not updated, you need something like this:

      const Time = require('Time'); const interval = Time.setInterval(func(), 500);