问题
I'm trying to come up with a method for calculating frequency of an action, to be more specific, mouse clicks.
This is what I have in my head (im not the best a maths or explaining but I'll try).
I expect a user to be able to reach an average click frequency of 10 clicks per second and I want to know what percentage of that target was achieved per 1 10th of a second. I am nearly there... I think but because I set the mouse clicks back down to 0, the frequency drops straight down instead of declining gradually...
This is where I'm at but I'm not thinking clear at the moment :-)
var frequency = function( maxFrequency, milliseconds, callback ) {
var mouseDown = 0;
var loopCount = 1;
var frequentTap = new taps( $(window), 'frequency', function() {
mouseDown++;
});
var loop = setInterval( function() {
callback( mouseDown / ( maxFrequency ) );
if( loopCount % 10 === 0 ) mouseDown = 0;
loopCount++;
}, milliseconds );
this.stop = function(){
clearInterval( loop );
}
};
frequency( 10, 100, function( freq ) {
console.log(freq);
});
dont worry about the tabs function, just assume it tracks clicks.
any help is very much appreciated
regards
EDIT:
created a plugin if anyone requires it: http://luke.sno.wden.co.uk/v8
回答1:
I was thinking about this in a slightly different way--you could store timestamps at which the mouse was clicked, and then do your calculation based on the timestamps you have stored.
Runnable example below:
var frequency = function(maxFrequency, milliseconds, updateFrequency, callback ) {
var timeTotal = 0;
var clickTimes = [];
var numberOfMilliseconds = milliseconds;
document.addEventListener("click", function() {
var currentTime = (new Date()).getTime(); //get time in ms
//add the current time to the array and the counter
timeTotal += currentTime;
clickTimes.push(currentTime);
});
setInterval(function() {
var currentTime = (new Date()).getTime(); //get time in ms
//remove any items that occurred more than milliseconds limit ago
while(clickTimes.length && (currentTime - clickTimes[0] > milliseconds)) {
timeTotal -= clickTimes.shift();
}
if (clickTimes.length > 0) {
numberOfMilliseconds = clickTimes[clickTimes.length - 1] - clickTimes[0];
} else {
numberOfMilliseconds = milliseconds;
}
callback(numberOfMilliseconds, clickTimes.length, (clickTimes.length * 100 / maxFrequency).toFixed(0));
}, updateFrequency);
};
frequency(10, 1000, 100, function(milliseconds, numberOfClicks, targetPercentage) {
document.getElementById("numberOfMilliseconds").innerHTML = milliseconds;
document.getElementById("numberOfClicks").innerHTML = numberOfClicks;
document.getElementById("targetPercentage").innerHTML = targetPercentage;
});
<h1>Click anywhere in the document</h1>
You've clicked <span id="numberOfClicks">0</span> times in the past <span id="numberOfMilliseconds">0</span> milliseconds<br/>
This is <span id="targetPercentage">0</span>% of your target.
回答2:
Right, I'm not sure on the math on this one but I think I have managed to achieve the desired effect here:
var frequency = function(actionTarget, milliseconds, callback) {
var mouseDown = 0;
$(window).click(function() {
if (mouseDown < actionTarget) mouseDown++;
});
var loop = setInterval(function() {
callback(mouseDown / (actionTarget));
mouseDown -= (1 - (actionTarget / milliseconds));
if (mouseDown < 0) mouseDown = 0;
}, milliseconds);
this.stop = function() {
clearInterval(loop);
}
};
jQuery(document).ready(function($) {
frequency(10, 100, function(freq) {
document.getElementById("result").innerHTML = Number(freq).toFixed(2);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<div id="result"></div>
来源:https://stackoverflow.com/questions/26495225/click-frequency-calculation