javascript - 如何通过 setTimeout() 打破使用标签
问题描述
这是我的代码:
mylabel:{
var myTimeout = setTimeout(function(){
SpinnerDialog.hide();
alert("Timeout");
break mylabel;
}, 10000);
connectWifi();
}
如果 Wi-Fi 网络不可用,我有一个功能connectWifi()
可能会挂起,所以我想在固定时间后停止它。此函数包含其他函数,它执行以下操作:
function(){
function(){
function(){
}
}
}
当调用超时函数时,我收到一条错误消息“未定义标签”
编辑:我的目标不是停止超时功能,而是停止 connectWifi 功能。
如果您想查看 connectWifi() 函数,这里是代码(我正在使用以下 cordova 插件:WifiManager、autowifi 和 CordovaMqTTPlugin):
function connectWifi() {
cordova.plugins.WifiManager.isWifiEnabled(function (err, wifiEnabled) {
console.log("isWifiEnabled result : " + wifiEnabled);
if (wifiEnabled == false) {
cordova.plugins.WifiManager.setWifiEnabled(true, function (err, succes) { console.log(err, succes); });
console.log("-- Wifi Enabled --");
}
});
window.plugins.autowifi.setssid(s_SSID, s_PASS);
SpinnerDialog.show(null, "Connexion en cours...", true);
window.plugins.autowifi.connect(function (scc) {
wifiReady = true;
SpinnerDialog.hide();
console.log("Wifi connected");
console.log("debut fonction connectMQTT");
if (wifiReady) {
//Connexion MQTT
SpinnerDialog.show(null, "Acces au serveur en cours...", true);
cordova.plugins.CordovaMqTTPlugin.connect({
url: _url, //a public broker used for testing purposes only. Try using a self hosted broker for production.
port: _port,
clientId: _clientId,
success: function (s) {
alert("Connexion reussie !");
connected = true;
console.log(JSON.stringify(s));
document.getElementById("connect").style.display = "none";
document.getElementById("disconnect").style.display = "initial";
document.getElementById("activity").innerHTML += "--> Success: you are connected to, " + _url + ":" + _port + "<br>"
SpinnerDialog.hide();
SpinnerDialog.show(null, "Recuperation des donnees...", true);
//Abonnement MQTT au topic
cordova.plugins.CordovaMqTTPlugin.subscribe({
topic: _topic,
qos: 0,
success: function (s) {
SpinnerDialog.hide();
document.getElementById("activity").innerHTML += "--> Success: you are subscribed to the topic, " + _topic + "<br>"
cordova.plugins.CordovaMqTTPlugin.listen(_topic, function (payload, params, topic, topic_pattern) {
//params will be an empty object if topic pattern is NOT used.
document.getElementById("activity").innerHTML += "--> Payload for" + topic + " topic: " + JSON.stringify(payload) + "<br>"
})
},
error: function (e) {
SpinnerDialog.hide();
document.getElementById("activity").innerHTML += "--> Error: something is wrong when subscribing to this topic, " + e + "<br>";
document.getElementById("subscribe").style.display = "initial";
document.getElementById("unsubscribe").style.display = "none";
//alert("err!! something is wrong. check the console")
console.log(e);
}
});
//---------------------
},
error: function (e) {
SpinnerDialog.hide();
connected = false;
//document.getElementById('status').innerHTML = "déconnecté";
//document.getElementById('statusbar').className = "w3-bar w3-red"
alert("Echec de la connexion, assurez-vous que vous êtes connectés en wifi au serveur puis veuillez reessayer");
document.getElementById("activity").innerHTML += "--> Error: something is wrong,\n " + JSON.stringify(e) + "<br>";
document.getElementById("connect").style.display = "initial";
document.getElementById("disconnect").style.display = "none";
alert("err!! something is wrong. check the console")
console.log(e);
},
onConnectionLost: function (e) {
SpinnerDialog.hide();
console.log(JSON.stringify(e));
connected = false;
//document.getElementById('status').innerHTML = "déconnecté";
//document.getElementById('statusbar').className = "w3-bar w3-red"
alert("Vous avez ete deconnecte");
document.getElementById("activity").innerHTML += "--> You got disconnected";
document.getElementById("connect").style.display = "initial";
document.getElementById("disconnect").style.display = "none";
}
});
//--------------------
} else {
console.log("wifiReady false");
}
});
}
(因为我是法国人,所以很少有法语的东西)
解决方案
出现break
在标记语句中声明的匿名函数中,但它本身不是语句的一部分。这就是为什么您不能从匿名函数跳转到标签的原因。
一些讨论您的实际问题的问题(如何从计时器取消活动):
您会发现此时没有简单的内置方法。
推荐阅读
- android - ConstraintLayout中的右文本视图,文本被切断
- java - 从键盘输入更新的更新方法不喜欢方法调用?
- java - 用相等的字符分割字符串
- jqgrid - 免费 jqGrid 和 ace admin 模板标题显示问题
- r - Plotly - 创建具有连续色标的饼图
- html - Vue.js v-bind:style 伪元素 :: 在内容图标之后
- python - TypeError: ufunc 'true_divide' 输出(类型码'd')不能被强制转换为提供的输出参数(类型码'q')
- python - Django 表单字段上的验证器给出属性错误
- symfony - Symfony 3.4 CLI:命令“doctrine:generate:entity”未定义
- r - 在 R API 函数调用中将参数名称设置为变量