javascript - 如何在html中的函数内部调用函数?

标签 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>

最佳答案

您可以使用事件,从HTML调用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");
}

但请确保在 stop 函数之前调用 download 函数。

关于javascript - 如何在html中的函数内部调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54455332/

相关文章:

node.js - nodeman 寻找 .meta 文件

javascript - 将一张图像叠加在另一张图像上

c# - div 标题的客户端更改未反射(reflect)在后面的 C# 代码中

javascript - 在 Javascript 中读取签名的 16 位数据

html - 如何在 css background-image : url(. ....) 中放入 div 2 图像

html - 如何使网站垂直响应?

html - 意外的重叠部分

node.js - 计算值与数组不匹配的位置

javascript - 在 iframe 预订表单中选择元素时出现问题

javascript - Raphael.js VSDOC