Skip to main content
Inspiring
February 8, 2018
Answered

Best way to call function from more than 1 event listener and pass parameter?

  • February 8, 2018
  • 1 reply
  • 205 views

I have a button bank that allows a quick search of a video component instance named "video0".  Rather than have a diff. function for each button, can I use one function and just pass to it the location I wish to seek to?  I tried this, but no luck...

_root.b1.addEventListener("click", govideo.bind(this,0));

_root.b2.addEventListener("click", govideo.bind(this,100));

_root.b3.addEventListener("click", govideo.bind(this,200));

function govideo(thetime) {

   _root.gotoAndPlay("video");

   video0.currentTime = thetime;

   $("#video0")[0].play();

}

    This topic has been closed for replies.
    Correct answer ChicoTom24

    Oops!  My bad...  The code below is correct.  The problem was my video placeholder was not long enough for the time values I had specified.  Thanks

    _root.b1.addEventListener("click", govideo.bind(this,0));

    _root.b2.addEventListener("click", govideo.bind(this,100));

    _root.b3.addEventListener("click", govideo.bind(this,200));

    function govideo(thetime) {

       _root.gotoAndPlay("video");

       video0.currentTime = thetime;

       $("#video0")[0].play();

    }

    1 reply

    Colin Holgate
    Inspiring
    February 8, 2018

    The code you showed seems to work exactly like you have it. At least as far as this line:

    function govideo(thetime) {

    There may be something going wrong after that. Where is video0 created? If it's a component you may need to do a getElementById() to get the video's reference.

    Also, is the frame "video" somewhere that those three buttons don't exist? If they're not present you could add some removeEventListeners before going to the "video" frame:

    function govideo(thetime) {

    _root.b1.removeEventListener("click", govideo);

    _root.b2.removeEventListener("click", govideo);

    _root.b3.removeEventListener("click", govideo);

    ChicoTom24AuthorCorrect answer
    Inspiring
    February 8, 2018

    Oops!  My bad...  The code below is correct.  The problem was my video placeholder was not long enough for the time values I had specified.  Thanks

    _root.b1.addEventListener("click", govideo.bind(this,0));

    _root.b2.addEventListener("click", govideo.bind(this,100));

    _root.b3.addEventListener("click", govideo.bind(this,200));

    function govideo(thetime) {

       _root.gotoAndPlay("video");

       video0.currentTime = thetime;

       $("#video0")[0].play();

    }