如何记录用户点击之间的时间并在达到 10000 次点击后将其导出为数组?
How to log the time between user clicks and export that as an array once they reach 10000 clicks?
我的代码不起作用
(使用 jquery):
$('button').click((function() {
var clicks = 0; clicks = clicks + 1; var history = [], last = +new Date();
return function e() {
history.push(e.timeStamp - last);
console.log(history[history.length - 1]);
last = e.timeStamp;}
return function c(){
if (clicks == 10000){
alert("10k clicks reached");
alert(history);
}
}
//Tested with (clicks == 1), it would keep on alerting me or wouldn't work at all
e();
c();
}
));
HTML
<button>Click me</click>
当我点击按钮时,它没有任何反应
有什么办法可以解决吗?
我原以为它会在达到一定点击量后提醒我并为我导出数组。
将最大点击次数更改为 3 以便于测试,请随意更改为 10000。
因为我们想要记录点击次数和点击次数,所以这些变量应该在点击处理程序之外,因为处理程序内的变量将在每次执行处理程序后被删除。
试试看:https://jsbin.com/lodifej/edit?html,js,output
let clicks = 0;
const timeBetweenClicks = [];
const prevClickTimestamp = [];
const clickHandler = () => {
const currentTime = Date.now();
const previousClickTime = prevClickTimestamp.length ? prevClickTimestamp[prevClickTimestamp.length - 1] : currentTime;
timeBetweenClicks.push(currentTime - previousClickTime);
// save current time stamp to calculate time difference the next time button is clicked
prevClickTimestamp.push(currentTime);
clicks += 1;
if (clicks === 3) {
alert("3 clicks reached. Time between clicks: " + timeBetweenClicks);
// remove event listener
$('button').off('click', clickHandler);
}
};
$('button').click(clickHandler);
备注
此代码导致第一个时间差值为 0。例如,对于 3 次点击,输出为 [0, 255, 1123]。如果这不是您想要的,可以在最后使用 timeBetweenClicks.shift()
将其删除。此外,这些值在 ms
.
中
我的代码不起作用 (使用 jquery):
$('button').click((function() {
var clicks = 0; clicks = clicks + 1; var history = [], last = +new Date();
return function e() {
history.push(e.timeStamp - last);
console.log(history[history.length - 1]);
last = e.timeStamp;}
return function c(){
if (clicks == 10000){
alert("10k clicks reached");
alert(history);
}
}
//Tested with (clicks == 1), it would keep on alerting me or wouldn't work at all
e();
c();
}
));
HTML
<button>Click me</click>
当我点击按钮时,它没有任何反应
有什么办法可以解决吗?
我原以为它会在达到一定点击量后提醒我并为我导出数组。
将最大点击次数更改为 3 以便于测试,请随意更改为 10000。
因为我们想要记录点击次数和点击次数,所以这些变量应该在点击处理程序之外,因为处理程序内的变量将在每次执行处理程序后被删除。
试试看:https://jsbin.com/lodifej/edit?html,js,output
let clicks = 0;
const timeBetweenClicks = [];
const prevClickTimestamp = [];
const clickHandler = () => {
const currentTime = Date.now();
const previousClickTime = prevClickTimestamp.length ? prevClickTimestamp[prevClickTimestamp.length - 1] : currentTime;
timeBetweenClicks.push(currentTime - previousClickTime);
// save current time stamp to calculate time difference the next time button is clicked
prevClickTimestamp.push(currentTime);
clicks += 1;
if (clicks === 3) {
alert("3 clicks reached. Time between clicks: " + timeBetweenClicks);
// remove event listener
$('button').off('click', clickHandler);
}
};
$('button').click(clickHandler);
备注
此代码导致第一个时间差值为 0。例如,对于 3 次点击,输出为 [0, 255, 1123]。如果这不是您想要的,可以在最后使用 timeBetweenClicks.shift()
将其删除。此外,这些值在 ms
.