javascript - Check if global variable value has changed in last N seconds -
I am trying to write some javascript that the value of a global variable has changed in the last 10 seconds or not. My first idea is to use the setTimeout
as follows:
var glob = 0; // Global variables that can change the user by clicking a button / / The following code is whether the globe has var tmp_glob = glob in a 10-second period; SetTimeout (function (if (tmp_glob == globe) {console.log ("value is not changed.");}}, 10000);
My argument is that, After having 10 seconds, Javascript will check that glob
and tmp_gl
are still the same and if they are, this means the globe
< Code> tmp_glob has not been changed since. However, it always gives the truth, even if I change the glob
from the customer, I'm 100% sure that the globe
Why is my code not working? Alternatively, can I provide a better solution for what I am trying to accomplish?
Your code works just fine, but this is a very bad behavior and your problem may be overwritten in your tmp_glob
One way to make it even stronger is to have your setTimeout
must be attached to a function that is the owner of tmp_glob
variable:
function clock glob () {var tmp_g Lob = glob; Settimeout (function () {if (tmp_glob == glob) {console.log ("Value is not changed.");} Else {console.log ("value has changed"); tmp_glob = glob;}}, 10000 ); }
Now when you want to start monitoring, you call:
watchglob ();
But you will not be able to overwrite tmp_glob
externally.
See:
You started an instant work expression (IEEE) which you want to avoid defining the designated function for it:
(function () {var tmp_glob = glob; setTimeout (function () {if (tmp_glob == Glob) {console.log ( "value has not changed.");} Else {console.log ( "value has changed" ); Tmp_glob = glob;}}, 10000);}) ();
Comments
Post a Comment