如何在 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/