use timeout ID to stop callback

This commit is contained in:
Araxeus
2021-04-18 00:44:22 +03:00
parent b7b1316e70
commit 00468c7d0e

View File

@ -212,29 +212,60 @@ function promptCreateSelect() {
return dataElement; return dataElement;
} }
let pressed = false; let nextTimeoutID = null;
function multiFire(timer, scaleSpeed, callback, ...args) {
if (!pressed) { /* Function execute callback in 3 accelerated intervals based on timer.
return; * Terminated from document.onmouseup() that is registered from promptCreateCounter()
* @param {function} callback: function to execute
* @param {object} timer: {
* * time: First delay in miliseconds.
* * limit: First Speed Limit, gets divided by 2 after $20 calls. $number change exponentially
* * scaleSpeed: Speed change per tick on first acceleration
* }
* @param {int} stepArgs: argument for callback representing Initial steps per click, default to 1
* steps starts to increase when speed is too fast to notice
* @param {int} counter: used internally to decrease timer.limit
*/
function multiFire(callback, timer = { time: 500, scaleSpeed: 140, limit: 100 }, stepsArg = 1, counter = 0) {
callback(stepsArg);
const nextTimeout = timer.time
if (counter > 20) {
counter = 0 - stepsArg;
if (timer.limit > 1) {
timer.limit = timer.limit / 2;
} else {
stepsArg *= 2;
}
} }
if (timer > scaleSpeed) { if (timer.time != timer.limit) {
timer -= scaleSpeed; timer.time = timer.time > timer.limit ?
timer.time - timer.scaleSpeed :
timer.limit;
} }
callback(...args);
setTimeout(multiFire, timer, timer, scaleSpeed, callback, ...args) nextTimeoutID = setTimeout(
multiFire, //callback
nextTimeout, //timer
//multiFire args:
callback,
timer,
stepsArg,
counter + 1
)
} }
function createMinusButton(dataElement) { function createMinusButton(dataElement) {
function doMinus() { function doMinus(steps) {
dataElement.value = validateCounterInput(parseInt(dataElement.value) - 1); dataElement.value = validateCounterInput(parseInt(dataElement.value) - steps);
} }
const minusBtn = document.createElement("span"); const minusBtn = document.createElement("span");
minusBtn.textContent = "-"; minusBtn.textContent = "-";
minusBtn.classList.add("minus"); minusBtn.classList.add("minus");
if (promptOptions.counterOptions?.multiFire) { if (promptOptions.counterOptions?.multiFire) {
minusBtn.onmousedown = () => { minusBtn.onmousedown = () => {
pressed = true; multiFire(doMinus);
multiFire(500, 100, doMinus);
}; };
} else { } else {
@ -246,16 +277,15 @@ function createMinusButton(dataElement) {
} }
function createPlusButton(dataElement) { function createPlusButton(dataElement) {
function doPlus() { function doPlus(steps) {
dataElement.value = validateCounterInput(parseInt(dataElement.value) + 1); dataElement.value = validateCounterInput(parseInt(dataElement.value) + steps);
} }
const plusBtn = document.createElement("span"); const plusBtn = document.createElement("span");
plusBtn.textContent = "+"; plusBtn.textContent = "+";
plusBtn.classList.add("plus"); plusBtn.classList.add("plus");
if (promptOptions.counterOptions?.multiFire) { if (promptOptions.counterOptions?.multiFire) {
plusBtn.onmousedown = () => { plusBtn.onmousedown = () => {
pressed = true; multiFire(doPlus);
multiFire(500, 100, doPlus);
}; };
} else { } else {
plusBtn.onmousedown = () => { plusBtn.onmousedown = () => {
@ -268,7 +298,10 @@ function createPlusButton(dataElement) {
function promptCreateCounter() { function promptCreateCounter() {
if (promptOptions.counterOptions?.multiFire) { if (promptOptions.counterOptions?.multiFire) {
document.onmouseup = () => { document.onmouseup = () => {
pressed = false; if (!!nextTimeoutID) {
clearTimeout(nextTimeoutID)
nextTimeoutID = null;
}
}; };
} }
@ -284,12 +317,12 @@ function promptCreateCounter() {
function validateCounterInput(input) { function validateCounterInput(input) {
const min = promptOptions.counterOptions?.minimum; const min = promptOptions.counterOptions?.minimum;
const max = promptOptions.counterOptions?.maximum; const max = promptOptions.counterOptions?.maximum;
//note that !min/max would proc if min/max are 0
if (min !== undefined && input < min) { if (min !== null && min !== undefined && input < min) {
return min; return min;
} }
if (max !== undefined && input > max) { if (max !== null && max !== undefined && input > max) {
return max; return max;
} }