Javascript,变量不能通过函数来​​访问 [英] Javascript, Variable is not accesible by function

查看:118
本文介绍了Javascript,变量不能通过函数来​​访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个phonegap应用程序,当用户点击按钮时播放声音:

I made an phonegap application that plays an sound when the user clicks on the button:

 <button onclick="playAudio('test.mp3')">Play Some Audio</button>

但我想声音停止,当用户做doubleclick,不知何故我的代码不工作! Im是一个初学者在javascript,并认为我did not把变量=媒体在正确的位置。这是我的完整代码:

But i want that the sound stops when the user makes an doubleclick, somehow my code dont works! Im an beginner in javascript, and think the i didnt put the variable = media in the right position. Here is my full code:

<body>
     <h1>Playing Audio</h1>

    <button onclick="playAudio('test.mp3')">Play Some Audio</button>
    <script type="text/javascript" src="cordova-2.4.0.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        app.initialize();
        var playing = false;
        var media = null;


        function playAudio(src) {
            if (!playing) {
                if (device.platform == 'Android') {
                    src = '/android_asset/' + src;
                }

                var media = new Media(src);
                media.play();
                playing = true;
            } else {
                media.stop();
                playing = false;
            }
        }
    </script>
</body>


推荐答案

现在我发现了问题, p>

Now i found the problem, you cannot use

 media = new Media (src) 

而不是我有tu使用

 my_Media = new Media (src)

与媒体一词有一个相互关系!
但是@Alexandre说的是absolutley正确,这让我最后的结果!
感谢

There is an interreference with the word media! But what @Alexandre said was absolutley correct, and that brougt me to the final result! Thanks

这篇关于Javascript,变量不能通过函数来​​访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆