Tell me why without if-and the timer works fine, and with it - it does not start at all?

var sum = 0; var first = 0; var second = 0; function additionFunction() { document.getElementById('example').innerHTML = ""; document.getElementById('timerAddition').innerHTML = ""; first = Math.floor(Math.random() * 10); second = Math.floor(Math.random() * 10); if (right >= 1) { first = Math.floor(Math.random() * 15); second = Math.floor(Math.random() * 15); } sum = parseInt(first + second); document.getElementById('example').innerHTML = first.toString() + " + " + second.toString(); document.getElementById('timerAddition').innerHTML = sum + Math.floor(Math.random() * 8); timerAddition(); } var timerID = setInterval(timerAddition, 1000); function timerAddition() { var obj = document.getElementById('timerAddition'); obj.innerHTML--; if (obj.innerHTML < sum.innerHTML) { clearInterval(timerID); } } 
  • one
    Add html markup and where did the numeric value of sum suddenly innerHTML come from? - DaemonHK
  • if I'm not mistaken, you have obj.innerHTML perceived as text, try wherever it is to register + obj.innerHTML instead of obj.innerHTML - Sherzod Yorov

1 answer 1

 obj.innerHTML < sum.innerHTML 

Because here instead of numbers lines are compared. You can try this:

 +obj.innerHTML < +sum.innerHTML 

but it’s generally bad to degrade data in the markup.

  • So the timer goes, but it does not reach if. And this sum = parseInt (first + second); Sum of two numbers. Maybe in order to better understand what's what, you can look here: kurusa.zhecky.net/lesson_11 . I would be very grateful for your help - Bipa
  • @ Bipach, where did the sum come from? In your code it is not. - Qwertiy
  • This is a global variable that is used in a slightly large function. Now add. - Bipa
  • Here, on top, the comment does not fit. - Bipa