【发布时间】:2018-10-31 22:58:48
【问题描述】:
这是我的代码:
mylabel:{
var myTimeout = setTimeout(function(){
SpinnerDialog.hide();
alert("Timeout");
break mylabel;
}, 10000);
connectWifi();
}
我有一个函数connectWifi(),如果 Wi-Fi 网络不可用,它可能会挂起,所以我想在固定时间后停止它。此函数包含其他函数,它的作用如下:
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的目的是什么? -
你跳出循环,而不是函数。
-
@Alban 要停止超时功能吗?
-
我编辑了我的帖子以添加更多信息。我不知道它是否回答了你的问题@FINDarkside developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 它说我们可以使用中断来退出标记的块
标签: javascript settimeout break