gpt4 book ai didi

javascript - 单页应用程序 : Uncaught ReferenceError: myFunction is not defined

转载 作者:太空宇宙 更新时间:2023-11-04 01:32:25 25 4
gpt4 key购买 nike

我有一个简单的代码,它是一个单页应用程序。我有几个 html header ,每个 header 都分配给不同的类名。我正在尝试根据 websocket 接收到的信号显示/隐藏它们。此外,我已将 onclick 事件绑定(bind)到其中一些 header 。但是一旦我单击标题,它就会抛出一个错误,指出函数未定义。谁能告诉我我在这里做错了什么。

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MultiTask Performance Testing</title>

<style type="text/css">
.loading {
display: none;
}
.overview {
display: none;
}
.charge {
display: none;
}
.discharge {
display: none;
}
.active {
display: block;
}

</style>

</head>
<body>
<!-- LOADING SCREEN -->
<h1 class="loading active">Loading....</h1>

<!-- OVERVIEW SCREEN -->
<br />
<a id="screen1-charge" class="overview" onclick="myFunction()" href="#">enter loading mode</a>
<br />
<a id="screen1-discharge" class="overview" onclick="dischargeFunction()" href="#">enter discharge mode</a>

<!-- CHARGE SCREEN -->
<h1 class="charge">CHARGE</h1>

<!-- DISCHARGE SCREEN -->
<h1 class="discharge">DISCHARGE</h1>

</body>

<script src="vendor/jquery-1.10.1.min.js"></script>
<script src="socketHandler.js"></script>
<script>
document.onreadystatechange = function () {
if (document.readyState === 'complete') {
console.log("*** READY ***");
var socketController = new ReconnectingWebSocket("ws://localhost:1337");

function myFunction() {

console.log("###############INSIDE myFunction###########");
//socketController.sendMessage(JSON.stringify(["sig-request-enter-charge"]));
}

function dischargeFunction()
{
socketController.sendMessage(JSON.stringify(["sig-request-enter-discharge"]));
}

}
};
</script>

</html>

部分SocketHandler.js文件

function ReconnectingWebSocket(url, protocols) {
...
this.onmessage = function(event) {
var data = event.data;

try{
// If the string is UTF-8, this will work and not throw an error.
data= decodeURIComponent(escape(data));
}catch(e){ }

var msg = $.parseJSON(data);
console.log("*** *** *** Message received :" + msg[0]);

if (msg[0] == "error") {
console.error(msg[1]);
}
else if(msg[0] == "sig-app-state-changed")
{
console.log("*****inside else if statement");
var elements = document.getElementsByClassName('overview');
for (var i in elements) {
if (elements.hasOwnProperty(i)) {
elements[i].className = 'overview active';

}
}

var elementy = document.getElementsByClassName('loading active');
for(var i in elementy){
if(elementy.hasOwnProperty(i)){
elementy[i].className = 'loading';
}
}
}
else if(msg[0] == "sig-reply-enter-charge"){
var elements = document.getElementsByClassName('overview');
for (var i in elements) {
if (elements.hasOwnProperty(i)) {
elements[i].className = 'overview active';

}
}
var elementy = document.getElementsByClassName('charge');
for(var i in elementy){
if(elementy.hasOwnProperty(i)){
elementy[i].className = 'charge active';
}
}
}
else {
//SignalsController.parseMessage(msg);
console.log("inside else condition");
//console.log(msg[1])
console.log("--received--->", msg);
}
...
}

收到信号“sig-app-state-changed”后的样子 enter image description here

最佳答案

函数声明在当前作用域中创建一个变量。

您已在分配给 document.onreadystatechange 的匿名函数中声明了 function myFunction() {,因此只能从该函数内部访问它。

您正试图从该函数外部调用它,因此它不存在。


使用 addEventListener 绑定(bind)您的事件处理程序(并在正确的范围内执行)而不是使用 onclick 属性。

关于javascript - 单页应用程序 : Uncaught ReferenceError: myFunction is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46827928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com