Here is my code:
mylabel:{
var myTimeout = setTimeout(function(){
SpinnerDialog.hide();
alert("Timeout");
break mylabel;
}, 10000);
connectWifi();
}
I have a function connectWifi()
that can hang if the Wi-Fi network is not available so I want to stop it after a fixed amount of time. This function contains other functions, it does something like this:
function(){
function(){
function(){
}
}
}
When the timeout fonction is called I get an error saying "Undefined label"
EDIT: My goal is not to stop the timeout function is to stop the connectWifi function.
if you want to see the connectWifi() function here is the code (I'm using the following cordova plugins : WifiManager, autowifi and 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");
}
});
}
(Few things are in french because I'm french)