如何在HTML函数内部调用函数? [英] how to call function inside of the function in html?

查看:695
本文介绍了如何在HTML函数内部调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在HTML上调用"stop()"?

How can I call "stop()" at HTML?

它在downloadFile2()内部.

It's inside of 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>

推荐答案

您可以使用events,从HTML调用stop函数.

You can make use events, Call stop function from 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");
}

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

But make sure download function gets called before stop function.

这篇关于如何在HTML函数内部调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆