McMuffin McMuffin - 17 days ago 5
Javascript Question

How to use if statement with a span were the text is changing

I want to run a function when

var roll_time = $('#banner')[0].childNodes[0].textContent;
is
== "Rolling in 25.00..."
i have tried to do so with this code, but i do not get an output. The text in the
<span>
is changing every 10 millisecond.



setInterval(test, 10)

function test(roll_time) {

var roll_time = $('#banner')[0].childNodes[0].textContent;

if (roll_time == "Rolling in 25.20...") {
console.log(success) }
}

<span id="banner">Rolling in 25.20...</span>




Answer

Thats because you are trying to access the roll_time which populate the value during the load.

$('#banner')[0].childNodes[0].textContent is a string, not a reference and hence if you need to access the string at a later point of time, you need to explicitly read the value from the DOM.

setInterval(test, 10)

function test() {
  var roll_time = $('#banner')[0].childNodes[0].textContent;
  if (roll_time == "Rolling in 25.20...") {
    console.log("success");
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="banner">Rolling in 25.20...</span>

Comments