如何用公式检测变量的变化

时间:2017-08-03 00:23:29

标签: javascript variables

我有这个包含公式的变量(levelFormula),然后通过另一个变量的输出更新

     var points = 100,
         levelFormula = (Math.sqrt(2 * points + 255) - 5) / 10;

换句话说,如果 points 得到更新,那么 levelFormula 也是如此。问题是,如何检测 levelFormula 的值何时发生变化?

1 个答案:

答案 0 :(得分:1)

如评论中所述,

var previousLevelFormula; // should be separated with below scope

...
var points = 100; // I guess it's from function parameter, because it will be changed so you need detection changes of levelFormula
var levelFormula = (Math.sqrt(2 * points + 255) - 5) / 10;

if (previousLevelFormula && previousLevelFormula != levelFormula) {
  // it's changed
}

previousLevelFormula = levelFormula; // update previous value