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

JavaScript question

Participant ,
Aug 14, 2018 Aug 14, 2018

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!

Views

1.7K

Translate

Translate

Report

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.

Votes

Translate

Translate
Participant ,
Oct 05, 2018 Oct 05, 2018

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?

Votes

Translate

Translate

Report

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

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

    }

}

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Oops - the first if statement should read

if(jsVal < 00){

Sorry!

Votes

Translate

Translate

Report

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

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

Votes

Translate

Translate

Report

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
Community Expert ,
Oct 05, 2018 Oct 05, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

Thanks again, works like a charm!

Votes

Translate

Translate

Report

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