Adding a pause/delay to a counter

Hi,

Awhile back Toan created this code for me to make an incremental counter. This is working perfect thank you. However, I now need the counter to pause/delay for just a moment for 500 milliseconds when it reaches 50, 125, 250 and then not advance past 400. I’ve tried adding another setTimer but just can’t get it to work! Any help would be much appreciated. Many thanks in advance.

click_hold_count_2.approj (344 KB)

Hi,

Please see the updated script:

// count every countInterval seconds
// if mouse down time >= changeIncrementTime seconds, count up in increments of increment every countInterval
// if mouse down time < changeIncrementTime seconds, count up in increments of 1 every countInterval
function setupClickHold(buttonObject, countVariable, changeIncrementTime /*seconds*/, countInterval /*seconds*/, increment, pauseTime /*seconds*/) {
     $(buttonObject.node).off('mousedown.clickHold').on('mousedown.clickHold', function() {
        var mouseDownStartTime = Date.now();
		var updateVariable = function() {
            var now = Date.now();
            var variableValue = prez.variable(countVariable);
			// increment value
			var currentIncrement = 1;
            if (now - mouseDownStartTime >= 1000 * changeIncrementTime)
                currentIncrement = increment;
            variableValue += currentIncrement;
            // round to special values: 50, 125, 250, 400
            if (variableValue > 50 && variableValue < 50 + currentIncrement)
                variableValue = 50;
            else if (variableValue > 125 && variableValue < 125 + currentIncrement)
                variableValue = 125;
            else if (variableValue > 250 && variableValue < 250 + currentIncrement)
                variableValue = 250;
            else if (variableValue >= 400) {
                variableValue = 400;
                // don't increase over 400
				if (intervalId) {
					clearInterval(intervalId);
					intervalId = null;
				}
                $(this).off('mouseup.clickHold');
            }
			// set value to variable
            prez.variable(countVariable, variableValue);
			// pause pauseTime seconds at special values
            if (variableValue == 50 || variableValue == 125 || variableValue == 250) {
				if (intervalId) {
					clearInterval(intervalId);
					intervalId = null;
				}
                timeoutId = setTimeout(function() {
					updateVariable();
					timeoutId = null;
					intervalId = setInterval(updateVariable, countInterval * 1000);
				}, pauseTime * 1000);
            }
        };
		var timeoutId = null;
        var intervalId = setInterval(updateVariable, countInterval * 1000);
        $(this).one('mouseup.clickHold', function() {
			if (intervalId) {
				clearInterval(intervalId);
				intervalId = null;
			}
			if (timeoutId) {
				clearTimeout(timeoutId);
				timeoutId = null;
			}
        });
    });
}

// update parameter values as needed
setupClickHold(prez.object('Button_4'), 'clickHoldCount', 2, 0.5, 5, 2);

Regards

I’ve updated the script above, please check if it works as expected.

Regards

Thank you so much for your speedy responses. This problem has been driving me mad! It’s almost working. Is there a way I can control the delay/pause time separately, as i need to adjust it the increment speed at times? Also when its counting up, as times when i stop with a mouseup at 50, 125, 250 it won’t continue counting up when i press the mouse again. I really appreciate your help. Many thanks.

Hi,

I’ve added a parameter to the function to control the pause time, and make it continue counting after stopping at special values.

Regards

I can’t thank you enough, this works perfectly. I would have never got this far without your help. I really appreciate this. Many thanks indeed.

Kind regards,
Ian

1 Like

I don’t believe it, I thought this was done! My client has changed their firmware. When the ‘variableValue’ is = 0 (zero), ‘clickHoldCount’ should read as CONT (short for continuous) instead of 0. It never counts below CONT (0). I’ve tried doing it but again have messed up your lovely setup. Any help would be greatly appreciated.

Many thanks,
Ian

You can remove the clickHoldCount number variable in your project, then add it again with the type TEXT and the initial value CONT.
After that change the script as follows:

// look for this line
var variableValue = prez.variable(countVariable);

// replace the above line by this one:
var variableValue = parseInt(prez.variable(countVariable)) || 0;

Regards

WOW - That’s amazing! I find myself thanking you so much. I really appreciate your help. Again, thank you so so much :slight_smile: