I am trying to get a global object to change one of my variables in a callback function initialized by one of my own methods. The callback seems to work, but the variable does not seem to have been changed when testing the global variable.
Why does the global object not change? Are changes to the global object saved in any staging area until the callback function completes?
let obj;
function test_object_flag() {
console.log("is the timer finished? " + obj.timer_finished);
}
class TestClass {
constructor() {
this.timer_finished = false;
}
start_timer(ptr_callback_function) {
setTimeout(function() {
this.timer_finished = true;
ptr_callback_function();
}, 1000);
}
}
$( document ).ready(function() {
obj = new TestClass();
obj.start_timer(test_object_flag);
});
source
share