0

My Javascript/jQuery function is only running once and only the bottom part of the function is working. I can flip the order and then the other piece will work. What am I missing (using the Buzz audio API for the play and pause methods)?

var $playFromPlayerBar = $('.main-controls .play-pause');

var togglePlayFromPlayerBar = function(){
    if (currentSoundFile.pause() && $playFromPlayerBar.click){
        var $songNumberCell = getSongNumberCell(currentlyPlayingSongNumber);
        $songNumberCell.html(pauseButtonTemplate);
        $playFromPlayerBar.html(playerBarPauseButton);
        currentSoundFile.play();
    } 

    if (currentSoundFile.play() && $playFromPlayerBar.click){
        var $songNumberCell = getSongNumberCell(currentlyPlayingSongNumber);
        $songNumberCell.html(playButtonTemplate);
        $playFromPlayerBar.html(playerBarPlayButton);
        currentSoundFile.pause();
    }  

};

//load album when window loads
$(document).ready(function() {
    $playFromPlayerBar.click(togglePlayFromPlayerBar);
});
11
  • do you get any errors in console? Commented Oct 7, 2016 at 20:48
  • since you have placed the click function in document.ready it will be run only once when DOM is loaded Commented Oct 7, 2016 at 20:49
  • @ochi that is just fundamentally wrong Commented Oct 7, 2016 at 20:51
  • @charlietfl ok, I'll take it back :) Commented Oct 7, 2016 at 20:52
  • 1
    if you are rendering the toggle buttons again, you need to re-initiate the click event. The new elements you added to the DOM has no idea that there is a click event. Commented Oct 7, 2016 at 20:53

2 Answers 2

1

The binding may be at fault here, try something like this:

$(document).ready(function() {
    $playFromPlayerBar.click(function() {
        togglePlayFromPlayerBar();
    });
});

Also you need to call the click function, you're doing this in your conditional expressions:

$playFromPlayerBar.click

You should be doing this:

$playFromPlayerBar.click()
Sign up to request clarification or add additional context in comments.

3 Comments

@charlietfl it could work, anything's possible in javascript ;-)
the function does run again now (which is an improvement) however only the bottom portion of the function runs (which is the following): if (currentSoundFile.play() && $playFromPlayerBar.click){ var $songNumberCell = getSongNumberCell(currentlyPlayingSongNumber); $songNumberCell.html(playButtonTemplate); $playFromPlayerBar.html(playerBarPlayButton); currentSoundFile.pause(); }
@Rapid99 you need to call the click function in your if checks.
0

Aha!! I figured it out.

var togglePlayFromPlayerBar = function(){
    if ($playFromPlayerBar.click){
        var $songNumberCell =           getSongNumberCell(currentlyPlayingSongNumber);

        if (currentSoundFile.isPaused()){
            $songNumberCell.html(pauseButtonTemplate);
            $playFromPlayerBar.html(playerBarPauseButton);
        } else {
            $songNumberCell.html(playButtonTemplate);
            $playFromPlayerBar.html(playerBarPlayButton);
        }

        currentSoundFile.togglePlay();
    }
};


//load album when window loads
$(document).ready(function() {
    $playFromPlayerBar.click(togglePlayFromPlayerBar);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.