简体   繁体   中英

Java Script function only runs once

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM