简体   繁体   English

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

[英]how to call function inside of the function in html?

How can I call "stop()" at HTML? 如何在HTML上调用“ stop()”?

It's inside of downloadFile2(). 它在downloadFile2()内部。

js example 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 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>

You can make use events , Call stop function from HTML . 您可以使用HTML events ,呼叫stop功能。

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");
}

But make sure download function gets called before stop function. 但是请确保在stop功能之前调用download功能。

you can call function inside of another by this 你可以这样在另一个内部调用函数

 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 .
  });
}

Then call the child function by this 然后通过这个调用子函数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM