gpt4 book ai didi

javascript - 点击频率计算

转载 作者:行者123 更新时间:2023-11-30 12:34:35 26 4
gpt4 key购买 nike

我正在尝试想出一种计算 Action 频率的方法,更具体地说,是鼠标点击。

这就是我的想法(我不是最好的数学或解释但我会尝试)。

我希望用户能够达到每秒 10 次点击的平均点击频率,并且我想知道每十分之一秒达到该目标的百分比。我快到了......我想但是因为我将鼠标点击设置回0,频率直接下降而不是逐渐下降......

这就是我现在的位置,但我现在还没有想清楚 :-)

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);
});

不要担心选项卡功能,只需假设它跟踪点击即可。

非常感谢任何帮助

问候

编辑:

创建了一个插件,如果有人需要的话:http://luke.sno.wden.co.uk/v8

最佳答案

我考虑的方式略有不同——您可以存储单击鼠标的时间戳,然后根据您存储的时间戳进行计算。

下面的可运行示例:

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.

关于javascript - 点击频率计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26495225/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com