如何检查 <span> 标记之间的值是否未更改

How to check if a value isn't changed in between <span> tag

页面看起来像:

<html>
  <span id="somespan">1000</span>
</html>

somespan 的值每 1 或 2 分钟增加一次。

使用 Javascript/JQuery,我如何检查值是否相同或每 5 分钟增加一次。

我的意思是 16:00 的值为 1000,2 分钟后 16:02 的值为 1200。
我如何检查它是否已更改。

我想制作类似的东西:

var somespan = $("#somespan").text();

if(somespan isnt changed) {
  #console.log('still same.')    
}

我将使用此代码的平台是 Google Chrome -> Tampermonkey

您可以实施 MutationObserver,但这也可以。

var current_value = null;
setInterval( function() {
  if (current_value != null) {
    if (current_value != $('#somespan').text()) {
      console.log('Value has changed!');
    }
  }
  current_value = $('#somespan').text();
}, 1000 );

// For testing only
$('input').on('input', function() {
  $('#somespan').text( $(this).val() );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="somespan">1000</span>

<!-- For testing only -->
<br/>
<input type="text" value="1000">

通过 MutationObserver(无需轮询)

const observer = new MutationObserver(function(mutations) {
  for (const mutation of mutations) {
    if (mutation.type === 'childList') {
      console.log('Value has changed!')
    }
  }
});

observer.observe(document.querySelector('#somespan'), {childList: true});

// For testing only
document.querySelector('input').addEventListener('input', function() {
  document.querySelector('#somespan').innerHTML = this.value;
});
<span id="somespan">1000</span>

<!-- For testing only -->
<br/>
<input type="text" value="1000">

迷你阵列怎么样?保留先前的值并在添加每个新值时重置。

// Global Variables
var somespan = document.getElementById("somespan");
// Global Variables => Array Values
var somespan_arr = [];

// Interval / Time function to check if # changed every 5 seconds
setInterval(function(){
  somespan_arr.push(somespan.innerHTML); // Push the initial value into array
  if (somespan.innerHTML != somespan_arr[0]) { // Check if prev value is different
    somespan_arr = []; // Reset Array back to default or ""
    somespan_arr.push(somespan.innerHTML); // Push new value
    console.log("Doesn't Match!"); // Logging Results
    console.log(somespan_arr); // Logging Results
  } else {
    console.log("It matches!"); // Logging Results
  }
},5000); // 5000 => 5 seconds where 1000 = 1 second

请原谅任何错误,我是 Whosebug 的新手。