Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在html中调用函数内部的函数?_Javascript_Html_Node.js_Http - Fatal编程技术网

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

Javascript 如何在html中调用函数内部的函数?,javascript,html,node.js,http,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.paus

如何在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>
下载停止~!!!

您可以通过此

 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>
停止~!!!

您可以使用
事件
,从
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
函数。

针对您的问题,不要发布所有代码,只发布逻辑。@MehulPrajapatiI改变了整个问题!如果可以修改脚本,可以将
req
定义为全局变量(
var-req;
在函数之外),还可以将stop函数从下载函数中取出,以便直接调用。@AliSheikhpour我尝试了这两种方法。不起作用。当我将[req]定义为一个全局变量时,它不起作用,stop()也有Uncaught TypeError的错误:无法读取unfinedw的属性'pause',当我按照您的建议添加控制台日志时,它调用stop函数但不暂停下载,pause()不起作用,只需显示日志。检查您是否已正确发送标题,并检查您是否正确暂停下载。我可以建议您做什么,或者您可以为此查询创建其他问题如何!这非常有效。EventEmitter与请求对象配合得很好!