首页 > 解决方案 > 如何通过 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");
  }
  });
}

(因为我是法国人,所以很少有法语的东西)

标签: javascriptsettimeoutbreak

解决方案


出现break在标记语句中声明的匿名函数中,但它本身不是语句的一部分。这就是为什么您不能从匿名函数跳转到标签的原因。

一些讨论您的实际问题的问题(如何从计时器取消活动):

您会发现此时没有简单的内置方法。


推荐阅读