简体   繁体   English

用按钮onclick调用的Javascript函数,在onload时失败

[英]Javascript function called with button onclick, fails with onload

I am trying to use an external javascript function in the body tag : 我试图在body标签中使用外部javascript函数:

<!DOCTYPE html>

<script type="text/javascript" src="js/Long.min.js"></script>
<script type="text/javascript" src="js/ByteBufferAB.min.js"></script>
<script type="text/javascript" src="js/ProtoBuf.min.js"></script>
<script type="text/javascript" src="js/fs.js"></script>
<script type="text/javascript" src="js/bundle.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="cordova_plugins.js"></script>

</head>
<body onload ="heartbeat()">

<h2>A demo for showing the pub/sub between clients and the message     broker</h2> <h3>IoTGateway</h3>
Topic :
<input type="text" name="pub"/>
Message:
<input type="text" name="pub"/>
<button onclick="connect()">
    Connect
</button>
<br>
<button onclick="publish()">
    Publish
</button>
<br>

<h3>BluetoothID</h3> Topic:
<input type="text" name="sub"/>
<button onclick="heartbeat()">
    Subscribe
</button>
<br>
<p id = "test"></p>
<p id="sub"></p>
<script>
        function heartbeat() {
            alert("hello");
            try{
            MqttPlugin.subscribe({topic: "$EDC/plugin"});
            }
            catch(err){
            alert(err.message);
            }
        }
       /* try{
        MqttPlugin.heartbeat({topic: "$EDC/tum/B8:27:EB:A6:A9:8A/HEARTBEAT-V1/mqtt/heartbeat"});
        }
        catch(err){
        alert(err.message);
        }*/



        function publish() {

            MqttPlugin.publish({
            topic:"$EDC/plugin",
            data:"Mqtt data"

            });
        }

        function subscribe() {
            MqttPlugin.subscribe({topic: "$EDC/plugin"});
        }



</script>
</body>
</html>

The function heartbeat is called when I call with the event onclick but fails with onload. 当我通过事件onclick进行调用时,将调用心跳函数,但由于onload失败。 In case of onload, it throws an error MqttPlugin not defined. 如果是onload,则会抛出未定义的错误MqttPlugin。 Is it because by the time it calls the function, the js files are not loaded? 是否因为在调用函数时未加载js文件? Could someone help me fix this? 有人可以帮我解决这个问题吗?

I believe that your basic aim here is to subscribe to a topic automatically when the page is loaded. 我相信您的基本目的是在页面加载后自动订阅主题。 You can either try: 您可以尝试:

</script>
<style onload="heartbeat()"></style>
</body>

Or try changing the order of imports. 或尝试更改导入顺序。

The first suggestion is just an hack not a full proof solution though. 但是,第一个建议只是破解,而不是完整的解决方案。

The basic problem is that the heartbeat function is getting called before cordova_plugins.js is getting loaded. 基本问题是在加载cordova_plugins.js之前会调用心跳函数。 So either delaying the function call or loading the file early will do the trick. 因此,延迟函数调用或尽早加载文件将达到目的。

Edited: 编辑:

This jQuery method might just be the solution: 此jQuery方法可能只是解决方案:

$( window ).load()

Please find documentation here . 请在此处找到文档。

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

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