D
D
des1roer2015-09-29 12:19:39
JavaScript
des1roer, 2015-09-29 12:19:39

js event timer?

tell me how to organize a simple cycle?

while (hlt1 > 0 && hlt2 > 0)
    {
        setTimeout(function () {
            hlt1 -= atk2;
            hlt2 -= atk1;
            console.log(hlt1, hlt2)
        }, 1000);  
    }

I realized about a dofiga of timers when the browser put ... I see two approaches - which one is more correct? or is it a matter of taste?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Ineshin, 2015-09-29
@des1roer

whilenot needed here. You need to do this: jsfiddle.net/7m6waxLm

var hlt1 = 10,
    hlt2 = 10;

function change() {
    hlt1 -= 1;
    hlt2 -= 1;
    
    var inProgress = hlt1 > 0 && hlt2 > 0;
    
    console.log(hlt1, hlt2, inProgress);
    
    if (inProgress) {
        setTimeout(change, 1000);
    }
}

setTimeout(change, 1000);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question