如何调用该函数的功能里面的html?(how to call function inside of

2019-11-05 11:37发布

我怎么能叫“STOP()”在HTML?

这是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>

Answer 1:

您可以使用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功能被称为前stop功能。



Answer 2:

你可以通过这个调用另一个内部功能

 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>


文章来源: how to call function inside of the function in html?