Страница выглядит так:
<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.info('still same.')
}
Платформа, на которой я буду использовать этот код, - это Google Chrome -> Tampermonkey.
У вас есть контроль над кодом, который в первую очередь изменяет диапазон?



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Вы можете реализовать MutationObserver, но это тоже подойдет.
var current_value = null;
setInterval( function() {
if (current_value != null) {
if (current_value != $('#somespan').text()) {
console.info('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">const observer = new MutationObserver(function(mutations) {
for (const mutation of mutations) {
if (mutation.type === 'childList') {
console.info('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.info("Doesn't Match!"); // Logging Results
console.info(somespan_arr); // Logging Results
} else {
console.info("It matches!"); // Logging Results
}
},5000); // 5000 => 5 seconds where 1000 = 1 second
Прошу прощения за допущенные ошибки, я новичок в Stackoverflow.
Вы можете использовать DOMSubtreeModified как $ ('# somespan'). On ('DOMSubtreeModified', function () {alert ('Значение изменено')})