简体   繁体   English

Javascript,函数无法使用变量

[英]Javascript, Variable is not accesible by function

I made an phonegap application that plays an sound when the user clicks on the button: 我制作了一个phonegap应用程序,该应用程序在用户单击按钮时播放声音:

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

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. 我是javascript的初学者,并认为我没有将变量= media放在正确的位置。 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>

Change the following line 更改以下行

var media = new Media(src);

to

media = new Media(src);

Now i found the problem, you cannot use 现在我发现了问题,您不能使用

 media = new Media (src) 

instead i had tu use 相反,我有涂用

 my_Media = new Media (src)

There is an interreference with the word media! 媒体一词引起了共鸣! But what @Alexandre said was absolutley correct, and that brougt me to the final result! 但是@Alexandre说的是绝对正确的,这使我对最终结果感到震惊! Thanks 谢谢

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

相关问题 全局声明的变量在javascript函数中是不可访问的? - globally declared variable is not accesible i n javascript function? javascript中的另一个函数无法访问数组值 - Array value not accesible in another function in javascript 如何维护和访问可从所有页面访问的javascript变量? - How to maintain and access javascript variable which is accesible from all the pages? 从模块导出的变量在 angular 类的函数内不可访问 - Variable exported from a module not accesible inside function of class in angular JavaScript对象中无法使用的原型 - Not accesible propotype in javascript object 如何存储$ http从外部$ http函数(Angularjs)获取变量可访问的响应? - How store $http get response in variable accesible from outside $http function (Angularjs)? 循环中的javarscript“可变变量可以从闭包中获取” - javarscript “mutable variable is accesible from closure” in loop javascript函数in in function in variable - javascript function with in function in variable 如何制作一个可供 React 组件访问且需要异步数据的变量才有用? - How to make a variable that is accesible to React component and needs asyncronous data to be useful? 如何通过javascript访问静态网页的某些元素(无法访问html)? - How to access certain element of a static webpage(html not accesible) through javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM