简体   繁体   English

Java Script函数仅运行一次

[英]Java Script function only runs once

My Javascript/jQuery function is only running once and only the bottom part of the function is working. 我的Javascript / jQuery函数仅运行一次,并且仅该函数的底部起作用。 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)? 我缺少什么(将Buzz音频API用于播放和暂停方法)?

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: 另外,您需要调用click函数,这是在条件表达式中执行的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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