0

I want to log the rolls but each time I run this code I get values each second (or as often as it is set at the interval). I want a way so that I can take log whenever the value changes, instead of getting it each second.

var red, black;

function mainOnRed() {
    switch (checkMainOnRed()) {
        case "red":
            console.log("Black");
            break;
        case "black":
            console.log("Red");
            break;
    }
};


function checkMainOnRed() {
    if (currentColor == "black" && rowSet.length == 2) {
        return "red";
    } else {
        return "black";
    }
}

setInterval(mainOnRed, 1000);

Values:

currentColor

and

rowSet

are defined in other parts of the script.

Klajdi
  • 337
  • 1
  • 10

2 Answers2

0

It all depends on when currentColor is set.

If it is set on an event you can just call mainOnRed for every event that changes that variable value.

Tuvia
  • 859
  • 4
  • 15
  • first is based on `first = String($(".class").slice(0).html());` and `if (first >= 0 && first <= 100) { currentColor = "red"; } else { currentColor = "black"; }` – Klajdi Apr 18 '16 at 18:01
  • @Klajdi you may want to see answers here: http://stackoverflow.com/q/1759987/6220751 – Tuvia Apr 18 '16 at 18:02
0

I would do something like this:

var red, black, lastColor;

function mainOnRed() {
    var color = checkMainOnRed();
    if (color != lastColor) {
        console.log(color);
        lastColor = color;
    };
};


function checkMainOnRed() {
    if (currentColor == "black" && rowSet.length == 2) {
        return "red";
    } else {
        return "black";
    }
}

setInterval(mainOnRed, 1000);
vasekhlav
  • 419
  • 4
  • 12
  • I do get the overall idea. Although your code doesnt work ill try to do what im thinking – Klajdi Apr 18 '16 at 18:19
  • Nvm, I was thinking to change value on occurrence to current value, and then check if values matched or not, and if not, check current value and change it. But your code did the trick, all you need is to run `checkMainOnRed` on `mainOnRed` so it would check the value aswell. `function mainOnRed() { checkMainOnRed();` It has been running for a while now and working fine, and I did adapt it to a value rather than a set of values (using `String` and `html()` (and if needed `slice()`) instead of using the function in `color` if anyone is looking for changes on characters. – Klajdi Apr 18 '16 at 18:37
  • I'm glad I helped :) – vasekhlav Apr 18 '16 at 18:41