我正在创建一个电子应用程序,可以在网站上下载多个页面。我希望能够通过立即终止功能来在每次按下“停止”按钮时停止下载页面。我尝试通过将功能设置为新功能来停止该功能,但是由于下载是在功能循环中进行的,因此它并未停止(我不确定这种方法是否还会停止startDownload功能)。
有没有一种方法可以轻松地停止执行函数,而不会停止整个脚本?
编辑:如果没有办法停止执行函数,是否有办法向NodeJS请求发送消息并停止它?
编辑2:NodeJS请求具有中止方法,但是我不确定如何告诉函数中止请求?
startDownload('website.com');
startDownload(url) {
var startAt = 0;
var maxPages = 15;
download(url, startAt, maxPages);
}
download(url, page, maxPages) {
if (page == maxPages) { finishDownload(url); return; }
request(url + '?p=' + page, (error, response, html) => {
downloadPage(html);
download(url, page + 1, maxPages);
}).catch((error) => {
finishDownload(url, 'Failed to download');
});
}
finishDownload(url, error = undefined) {
if (!error) {
alert(url + ' finished downloading');
} else {
alert(url + error);
}
}
$(document).on('click', '#stopDownload', function() {
var downloadFunction = startDownload;
startDownload = function() {return false};
startDownload = downloadFunction;
alert('download stopped by killing function');
});
答案 0 :(得分:0)
是的,您可以停留
<!DOCTYPE html>
<html>
<body>
<div id="table" style="display:block">
<div class="container text-center">
<div class="row">
<div class="col-sm-4">
<div class="panel panel-primary">
<div class="panel-heading">Reportees List</div>
<table id="employee_table" class="table table-hover" cellspacing="0" width="100%" cellspacing="0">
<tr>
<th>Number</th>
<th>Name</th>
<th>UserId</th>
<th>Count</th>
<th>Number</th>
<th>JiraNumber</th>
<th>JiraStatus</th>
<th>EmailId</th>
</tr>
<tr>
<td>1</td>
<td> Ag</td>
<td>1</td>
<td>Raj </td>
<td>0</td>
<td>1 </td>
<td>TR0-150</td>
<td>GMAIL </td>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<div style="text-align:right; width:100%; padding:0;">
<button id="sendMail" class="btn btn-primary btn-lg pull-right" style="color: white; background-color: blue;padding:5px" onclick="sendMail()">SendMail</button>
</div>
</div>
</body>
</html>