Skip to content

Instantly share code, notes, and snippets.

@IbrahimTanyalcin
Last active February 26, 2018 18:08
Show Gist options
  • Save IbrahimTanyalcin/d7efe01a7950032316a2b102c71b18a9 to your computer and use it in GitHub Desktop.
Save IbrahimTanyalcin/d7efe01a7950032316a2b102c71b18a9 to your computer and use it in GitHub Desktop.
linkedin help - 20180222 - 1
var obj = {value:3,stop:false}, //assing your value to obj.value every sec etc.
values = [], //array to store values
freq = 500, //frequency of check
result = undefined; //the result of average in the end
function checker(t){ //the function to periodically check
checker.startTime = checker.startTime || t;
if(obj.stop){ //if you set obj.stop to true, you will terminate the check
/*in the end calculate the result
if you want, store the a.length somewhere else
to prevent property access each time, but I won't
bother with that here*/
return result = values.reduce(function(ac,d,i,a){if(i !== a.length -1){return ac += d}else{return (ac + d)/a.length}},0);
}
if(t - checker.startTime >= 500) {
checker.startTime = t;
values.push(obj.value);
}
window.requestAnimationFrame(checker);
}
/*To use do check(); to stop set obj.stop to true,
after you stop the result variable will have the average*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment