【问题标题】:SoundCloud Getters - getDurationSoundCloud 吸气剂 - getDuration
【发布时间】:2014-02-16 16:11:58
【问题描述】:

我正在尝试编写一个脚本,一旦按下按钮,它将显示来自 SoundCloud 的歌曲的持续时间。我在 API 上也遇到了一些麻烦。我已经阅读并重新阅读了有关某些“回调”传递参数的行,但我仍然不明白。 “正因为如此,每个 getter 方法都接受一个回调函数作为参数,当调用该参数时,将获得 getter 方法的返回值。”

这是我的代码所在。我知道它不漂亮,但我只是想让这该死的东西发挥作用。

<!doctype html>
    <html>
    <head>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
      <script src="http://w.soundcloud.com/player/api.js"></script>
      <script>
       $(document).ready(function() {
         var widget = SC.Widget(document.getElementById('playing'));
         widget.bind(SC.Widget.Events.READY, function() {
           console.log('Ready...');
         });

         $('button').click(function() {
           widget.getDuration(function(sound));
         });
       });

       function sound(var time) {
        document.write(time);
       }
      </script>
    </head>
    <body>
                <iframe id="playing" 
                width="500" 
                height="400" scrolling="no" 
                frameborder="yes" 
                src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/66816173&amp;auto_play=false&amp;hide_related=false&amp;visual=true">
                </iframe>
      <button>Play / Pause</button>
    </body>
    </html>

【问题讨论】:

    标签: javascript soundcloud


    【解决方案1】:

    您的代码中有两个错误的函数定义。 在 developer.mozilla.org 上,您可以找到一些非常好的 javascript 文档。 特别是,在此页面上,您可以了解如何正确定义函数: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

    回到你的代码:

    1) getDuration soundcloud 函数接受回调。 function(sound) 不是有效的函数定义。

    2) 函数定义不需要参数名称前的var关键字

    这是您的代码,其中包含两个更正。它可以按您的预期工作。

    var widget = SC.Widget(document.getElementById('playing'));
        widget.bind(SC.Widget.Events.READY, function() {
            console.log('Ready...');
        });
    
        $('button').click(function() {
            widget.getDuration(function(duration){
                alert(duration);
            });
        });
    });
    

    在这里你可以看到正在工作的小提琴:http://jsfiddle.net/Ldc2N/

    还有一件事:我看到在应该是回调的地方,你调用了document.write

    function sound(time) { document.write(time); }
    

    请注意,它只是在文档尚未准备好时才将内容附加到文档中。当文档完全准备好时调用 int 会覆盖其内容。

    【讨论】:

    • 啊,好吧。惊人的!非常感谢!
    • 不客气。如果您发现答案有用,请考虑投票(这就是 stackoverflow 的工作方式;-))。谢谢。
    • 如果没有按钮,我该怎么做呢?页面一加载,弹出窗口就会出现?
    • 通过在小部件准备好后立即调用 getDuration 方法 - 即在 console.log('Ready...') 下。 jsfiddle.net/Ldc2N/2
    猜你喜欢
    • 2018-11-16
    • 2018-04-05
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 2023-02-02
    • 2014-02-24
    相关资源
    最近更新 更多