如何在html函数内部调用函数?

时间:2019-01-31 07:25:51

标签: javascript html node.js http

如何在HTML上调用“ stop()”?

它在downloadFile2()内部。

js示例

  function download() {
      var req = request({
        method: 'GET',
        uri: url
      });
      var out = fs.createWriteStream(path);
      req.pipe(out);

      function stop(){
       req.pause();
      }

      req.on('response', function(data) {
      });
      req.on('data', function(chunk) {
        stop();//I want to execute this method at html .
      });
    }

html

  <tr><td><a class="checkBtn" onclick="downloadFile2(event)">download</a></td><td><a class="checkBtn" onclick="downloadFile2.innerfunc();" value="ACTION">stop~!!!</a></td></tr>

2 个答案:

答案 0 :(得分:0)

您可以以此在另一个函数中调用

 function download() {
  var req = request({
    method: 'GET',
    uri: url
  });
  var out = fs.createWriteStream(path);
  req.pipe(out);

  this.stop = function stop(){
   req.pause();
  }

  req.on('response', function(data) {
  });
  req.on('data', function(chunk) {
    stop();//I want to execute this method at html .
  });
}

然后以此调用子函数

<td><a class="checkBtn" onclick="(new download()).stop();" value="ACTION">stop~!!!</a></td>

答案 1 :(得分:0)

您可以使用 String ret = ""; HttpURLConnection con = null; DbManager db = new DbManager(getApplicationContext()); try{ SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); String URL_STRING = pref.getString("STORED_URL_FOR_REST_SERVICE", null) + "/api/values/7"; URL Url = new URL (URL_STRING); con = (HttpURLConnection) Url.openConnection(); con.setRequestProperty("Connection", "Keep-Alive"); con.setRequestProperty("json_input", db.getJsonToSend()); con.setRequestMethod("POST"); //con.setDoInput(true); //con.setDoOutput(true); con.setUseCaches(false); int responseCode = con.getResponseCode(); if (responseCode == 200 || responseCode == 201 ) { BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line+"\n"); } br.close(); ret = sb.toString(); } else { ret = "ERRORE: " + Integer.toString(con.getResponseCode()); } }catch(Exception e){ ret = "ERRORE: " + e.getMessage(); } finally { if(con != null) { con.disconnect(); } } if(ret.contains("ERRORE")){ Toast.makeText(getApplicationContext(), "Errore nell'invio dei dati\nNuovo tentativo a breve", Toast.LENGTH_SHORT).show(); }else{ Gson gson = null; Rest1 r2 = null; try { gson = new Gson(); r2 = gson.fromJson(ret, Rest1.class); db = new DbManager(getApplicationContext()); }catch(Exception e){ Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); } try { db.setRowToSent(r2.getResult().split("#")); }catch(Exception e){ Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); } if (r2.getMessage().equals("OK")) { } else if (r2.getMessage().equals("KO")) { Toast.makeText(getApplicationContext(), "Errore nell'invio dei dati\nNuovo tentativo a breve", Toast.LENGTH_SHORT).show(); } } ,从events调用stop函数。

HTML

但是请确保在const events = require('events'); var EventEmitter = new events.EventEmitter(); function download() { var req = request({ method: 'GET', uri: url }); var out = fs.createWriteStream(path); req.pipe(out); EventEmitter.on("stop",function(){ req.pause(); }) req.on('response', function (data) { }); req.on('data', function (chunk) { stop();//I want to execute this method at html . }); } function stop() { EventEmitter.emit("stop"); } 函数之前调用download函数。