【问题标题】:Passing variable between functions in Javascript (Titanium Studio Desktop)在 Javascript 中的函数之间传递变量(Titanium Studio 桌面)
【发布时间】:2011-07-15 08:36:23
【问题描述】:

希望我能很好地表达这个问题。我正在 Titanium Desktop 中开发一个简单的音频播放器。我现在关注的主要代码如下:

function pickMusicFolder (){
    var win = Titanium.UI.getCurrentWindow();

    win.openFolderChooserDialog(function(folderResponse) {
        var file = Titanium.Filesystem.getFile(folderResponse[0]);
        var listing = file.getDirectoryListing();
        for (var i = 0; i < listing.length; i++) {
            if (listing[i].isDirectory()) {
                // if the listing is a directory, skip over it
                $('#main').append('DIRECTORY: ' + listing[i].nativePath() +
                    '<br />');
                // continue;
            }
            else {
                // otherwise, print the filename of the file to the #main content window
                var songOnList = listing[i].nativePath();
                var songURL = songOnList.replace(/\\/g,"/");
                $('#main ul').append('<li><a href="javascript:playSong(\'' + songURL + '\')">' + songURL + '</a></li>');
            }
        }
    });
};

function playSong(songURL){
    var currentSong = Titanium.Media.createSound(songURL);
    currentSong.play();

    this.stopPlayback = stopPlayback;

    function stopPlayback(currentSong){
        currentSong.stop();
    }
}

然后是相关的HTML:

<input type="image" src="img/player_stop.png" name="stopPlayback" onClick="playSong.stopPlayback(songURL)" />
<input type="image" src="img/folder_add.png" name="pickMusicFolder" onClick="pickMusicFolder()" />

现在,pickMusicFolder 和 playSong 本身都可以正常工作。但是,stopPlayback 不起作用,我很难掌握如何处理播放和停止音频的不同功能,因为生成可点击链接的代码在 pickMusicFolder 函数中完全划分,而用于停止的代码播放仅附加到一个单独的界面按钮。

我只需要访问多个函数之间的 songURL 变量,以便能够在播放(或不播放)一首单独的歌曲时对其执行操作。我正在避免使用全局变量,因为我发现这有点逃避现实。

有人有什么想法吗?非常感谢任何提示! (哦,请忽略我丑陋的代码;在发布之前尝试了一堆 hack-y 解决方案。)

【问题讨论】:

    标签: javascript jquery titanium appcelerator


    【解决方案1】:

    很快的解决方案是将currentSong存储为函数playSong的属性:

    function playSong(songURL){
      var currentSong = Titanium.Media.createSound(songURL);
      playSong.currentSong = currentSong; // store curernt playing song as property of function
      currentSong.play();
    }
    
    playSong.stopPlayback = function() {
      if (playSong.currentSong) {
        playSong.currentSong.stop();
        delete playSong.currentSong;
      }
    };
    

    当歌曲停止播放时,只需删除这个意味着现在没有歌曲正在播放的属性

    【讨论】:

    • 抱歉,我可能不太清楚:我需要我的“停止”按钮,在我的 HTML sn-p 中显示的代码,来停止播放当前正在播放的歌曲。我不完全确定将 songURL 属性附加到 playSong 对我有什么帮助?
    • 我正在编辑代码,在 playSong 函数的第二行中,我们存储在属性中播放的歌曲,并且在 stopPlayback 上使用此属性
    【解决方案2】:

    您应该使用闭包。见下文

    var stopPlayback = function() { /* Placeholder, defined later */ };
    
    var playSong = function(songURL){
      var currentSong = Titanium.Media.createSound(songURL);
      currentSong.play();
    
      // Redefine stopSong in the parent scope
      stopPlayback = function(){
        // currentSong is available to this function via closure
        currentSong.stop();
      }
    }
    

    HTML 编辑

    <input type="image" src="img/player_stop.png" name="stopPlayback" onClick="stopPlayback()" />
    

    附带说明,您不应该使用 HTML 属性来附加 JavaScript 事件。 This is a great series on event attachment,但您真正应该阅读的是this part。此外,每个 JS 库都提供了附加事件的方法,让您的生活更轻松。

    【讨论】:

    • 非常感谢。两个问题:1.)“占位符”区域究竟会发生什么?它是为某些特定功能而保存的吗? (或者你的意思是“稍后定义”就像“在 playSong 函数的范围内定义?”) 2.)所以你说不要生成 HTML 无序列表(包含 JS 事件)并且不要使用按钮输入上的 JS 事件?肯定会很快阅读这些链接;再次感谢。
    • 1) 稍后在 playSong 函数范围内定义。 2) 阅读链接,你就会明白我对事件的意思,但基本上,你的结构/数据 (HTML) 中不应该有逻辑 (JavaScript)。在您的 javascript 中连接事件,而不是在您的 HTML 中。
    猜你喜欢
    • 2011-06-03
    • 2012-04-28
    • 2020-07-02
    • 2012-07-19
    • 2012-10-31
    • 1970-01-01
    • 2013-04-09
    • 2013-03-07
    相关资源
    最近更新 更多