Working on some javascript. I found a pretty good function that calculates the speed of the cursor. The problem is that i want to return the actual value, not a callback. How would you do that?
function makeVelocityCalculator(e_init, callback) {
var x = e_init.clientX,
y = e_init.clientY,
t = Date.now();
return function(e) {
var new_x = e.clientX,
new_y = e.clientY,
new_t = Date.now();
var x_dist = new_x - x,
y_dist = new_y - y,
interval = new_t - t;
// update values:
x = new_x;
y = new_y;
t = new_t;
var velocity = Math.sqrt(x_dist*x_dist+y_dist*y_dist)/interval;
callback(velocity);
};
}
well , then change that function to return velocity, instead of "callback(velocity)"
Or you can use it the way it was intended
makeVelocityCalculator(initialEvent, function(velocity) {
console.log("velocity is", velocity);
});
is pretty much same as
var velocity = makeVelocityCalculator(initialEvent);
console.log("velocity is", velocity);