Copy link to clipboard
Copied
I want to build an interaction with JS. I want to use a button to increase the value of a number of a meter as long as the button is clicked and hold it. On release of the button the number has to stay at the last value. Pushing and holding it again has to further increae the value.
How to build this with Some JS in CP?
Help is very Welcome!
Sorry, I read it as it needs to only work once.
Remove btn.removeEventListener("mousedown",buttonPress,false) from the clearPress function.
Copy link to clipboard
Copied
One more challenge.
I would like the number (myVal) to always have 2 digits, so not 1, 2, 3 etc but 01, 02, 03 etc.
Any ideas?
Copy link to clipboard
Copied
Hmmm
You'll have to do the incrementing, then if the value is only a single digit, cast the value to a string before passing it back to Captivate. That'll need another variable in the js.
Something like this:
function buttonPressUp() { //prevents values higher than 99
var jsVal = parseInt(window.myVal); //read the value of myVal from CP and cast it to an integer
if (window.jsVal < 99) {
cp.hide("img1");
cp.show("img2");
timer = setInterval(function () {
jsVal++;
if(jsVal < 10){
window.myVal = "0" + jsVal.toString(); //cast jsVal to a string and append a zero in front
}
else {window.myVal = jsVal;
}
if (jsVal > 98) {
clearPress();
}
}, 100);
}
}
Copy link to clipboard
Copied
Oops - the first if statement should read
if(jsVal < 00){
Sorry!
Copy link to clipboard
Copied
Don't know why the system isn't letting me edity my own comments.
Let's try that again:
if(jsVal < 99){
Copy link to clipboard
Copied
You can only edit when it is the last one in a thread.
Copy link to clipboard
Copied
Thanks again, works like a charm!