0

In JAVASCRIPT:

If I have a variable which value is constantly changing (100+ times a second). How do I 'record' a specific value at a specific point in time? Added to this, how do I base this point in time off of another variable of which value has changed?

This needs to be strictly in JavaScript. I've looked at the onChange() method, but I'm unsure if I have to use this in conjunction with HTML for it to work. If not, could someone give me an example where this is not the case?

Cheers

2
  • 1
    well variables do not have a change method. What is the specific time? Question is very broad and hard to tell what you need. Commented Sep 21, 2019 at 3:57
  • You should clarify more. Thus, I can help more. Commented Sep 21, 2019 at 4:08

2 Answers 2

1

I'm not 100% clear on what you're trying to do, but as Ranjith says you can use setTimeout to run arbitrary code at some (approximate) future time.

This example could likely be improved if I had a bit more detail about what you're doing.

If you're in a node environment you might consider using an event emitter to broadcast changes instead of having to have the variable in scope. (This isn't particularly hard to do in a browser either if that's where you are.)

The html/css parts of this are just for displaying the values in the example; not necessary otherwise.

const rand = document.getElementById('rand');
const snapshot = document.getElementById('snapshot');

let volatile = 0;

// update the value every ~100ms
setInterval(() => {
  // assign a new random value
  volatile = Math.random();

  // display it so we can see what's going on
  rand.innerText = volatile;
}, 100);

// do whatever you want with the snapshotted value here
const snap = () => snapshot.innerText = volatile;

// grab the value every 2 seconds
setInterval(snap, 2000);
div {
  margin: 2rem;
}
<div>
  <div id="rand"></div>
  <div id="snapshot"></div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

Ok - well you can poll variable changes ... even though you can use setters...

Lets compare:

Polling:

let previous;
let watched = 0;
let changes = 0;
let snap = () => previous = watched !== previous && ++changes && watched || previous;
let polling = setInterval(snap, 100);

let delta = 1000 * 2
let start = Date.now();
let last = start;
let now;
let dt = 0
while(start + delta > Date.now()){
   now = Date.now();
   dt += now - last;
   last = now;
   if(dt > 100){
      watched++;
      dt = 0;
   }
} 
document.getElementsByTagName('h1')[0].innerText = (changes === 0 ? 0 : 100 * watched / changes) + "% hit" 
if(watched - changes === watched){
  throw Error("polling missed 100%");
}
<h1><h1>

emitting:

const dataChangeEvent = new Event("mutate");
const dataAccessEvent = new Event("access");
// set mock context - as it is needed
let ctx = document.createElement('span');

// add watchable variable
add('watched', 0);

//listen for changes
let changes = 0;
ctx.addEventListener('mutate', () => changes++);

let delta = 1000 * 2
let start = Date.now();
let last = start;
let now;
let dt = 0
while(start + delta > Date.now()){
   now = Date.now();
   dt += now - last;
   last = now;
   if(dt > 100){
      ctx.watched++;
      dt = 0;
   }
} 

document.getElementsByTagName('h1')[0].innerText = (changes === 0 ? 0 : 100 * ctx.watched / changes) + "% hit" 
if(ctx.watched - changes === ctx.watched){
  throw Error("trigger missed 100%");
}


function add(name, value){
  let store = value
  Object.defineProperty(ctx, name, {
    get(){
      ctx.dispatchEvent(dataAccessEvent, store)
      return store;
    },
    set(value){
      ctx.dispatchEvent(dataChangeEvent, {
        newVal: value,
        oldVal: store,
        stamp: Date.now()
      });
      store = value;
    }
  })
}
<h1></h1>

The usage of a while loop is on purpose.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.