Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

JavaScript question

Participant ,
Aug 14, 2018 Aug 14, 2018

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!

4.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

People's Champ , Aug 20, 2018 Aug 20, 2018

Sorry, I read it as it needs to only work once.

Remove btn.removeEventListener("mousedown",buttonPress,false) from the clearPress function.

Translate
Participant ,
Oct 05, 2018 Oct 05, 2018

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 05, 2018 Oct 05, 2018

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

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 05, 2018 Oct 05, 2018

Oops - the first if statement should read

if(jsVal < 00){

Sorry!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 05, 2018 Oct 05, 2018

Don't know why the system isn't letting me edity my own comments.

Let's try that again:

if(jsVal < 99){

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 05, 2018 Oct 05, 2018

You can only edit when it is the last one in a thread.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 06, 2018 Oct 06, 2018
LATEST

Thanks again, works like a charm!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Help resources