全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

IP归属甄别会员请立即修改密码
查看: 1215|回复: 17
打印 上一主题 下一主题

[经验] (已解决)大佬指教下js异步中如何添加延时

[复制链接]
跳转到指定楼层
1#
发表于 2022-3-6 21:00:28 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
本帖最后由 qingseyouran 于 2022-3-7 11:44 编辑

3.7号补充
使用for已经解决该问题,settimeout就是不行


谢谢几位坛朋的帮忙,不过还是不行。
只能弄个间接的,
把代码里的downloadPic(pageCurrent)删除,手动点可以下载一页,一直点可以顺序下载,到时再配合个自动化脚本。
有空再研究了。
有大佬的话,帮忙再研究研究。


下面代码下载太快了,过一会就会下载失败,想添加个延时,非码农,不懂js,网上找了半天弄不好,麻烦大佬指教下。

    if(document.location.pathname.indexOf("onlinebook")>0){
        document.body.oncontextmenu = ""
        var pageTotal = 0;
        var picUrl = ""
        //从0开始,为实际页码减一
        var pageCurrent = 0;
        //下载指定页面图片
        function downloadPic(page) {
            picUrl = path + "&pageNo=" + page ;
            fetch(picUrl).then(res => res.blob().then(blob => {
                var a = document.createElement('a');
                var url = window.URL.createObjectURL(blob);
                var filename = page + '.jpg';
                a.href = url;
                a.download = filename;
                a.click();
                window.URL.revokeObjectURL(url);
                if(pageCurrent<pageTotal){
                    pageCurrent++;
                    downloadPic(pageCurrent);
                }
            }))
        }
        //批量下载
        function batchDownload() {
            pageTotal = document.getElementById("sumNumb").innerText - 1;
            downloadPic(pageCurrent)
        }
        //创建下载按钮
        var downloadBtn = document.createElement("a");
        downloadBtn.innerText = "批量下载全书";
        downloadBtn.onclick = function () { batchDownload() };
        document.querySelector("body > div.divcenter > div.bq > div.bqmiddle > div.ml").appendChild(downloadBtn);
    }
推荐
发表于 2022-3-6 21:21:55 | 只看该作者
不管啥语言,最烦的就是匿名方法
18#
 楼主| 发表于 2022-3-7 11:45:37 | 只看该作者
3.9号,问题已经解决。
17#
 楼主| 发表于 2022-3-6 22:17:02 | 只看该作者
把代码里的downloadPic(pageCurrent)删除,手动点可以下载一页,一直点可以顺序下载,到时再配合个自动化脚本。
有空再研究了。
16#
 楼主| 发表于 2022-3-6 22:00:50 | 只看该作者

console提示
Uncaught ReferenceError: downloadPic is not defined
    at <anonymous>:1:1
15#
 楼主| 发表于 2022-3-6 21:56:29 | 只看该作者
darius 发表于 2022-3-6 21:38
替换fetch(picUrl).then(res => res.blob().then(blob => { 里的试下,能不能用我也不知道,因为我也是菜 ...

这个嵌套回调,完全看不懂
14#
 楼主| 发表于 2022-3-6 21:55:20 | 只看该作者
darius 发表于 2022-3-6 21:38
替换fetch(picUrl).then(res => res.blob().then(blob => { 里的试下,能不能用我也不知道,因为我也是菜 ...

            fetch(picUrl).then(res => res.blob().then(blob => {
new Promise((resolve, reject) => {
    setTimeout(() => {
        var a = document.createElement('a');
        var url = window.URL.createObjectURL(blob);
        var filename = page + '.jpg';
        a.href = url;
        a.download = filename;
        a.click();
        resolve()
    }, 1000);
}).then(_ => {
    window.URL.revokeObjectURL(url);
    if(pageCurrent<pageTotal){
        pageCurrent++;
        downloadPic(pageCurrent);
    }
})
            }))
        }

也只能下第一页
13#
 楼主| 发表于 2022-3-6 21:44:38 | 只看该作者
darius 发表于 2022-3-6 21:38
替换fetch(picUrl).then(res => res.blob().then(blob => { 里的试下,能不能用我也不知道,因为我也是菜 ...

好,马上试下
12#
 楼主| 发表于 2022-3-6 21:42:57 | 只看该作者
靓坤 发表于 2022-3-6 21:24
用settimeout把fetch那里包住即可

            setTimeout(fetch(picUrl).then(res => res.blob().then(blob => {
                var a = document.createElement('a');
                var url = window.URL.createObjectURL(blob);
                var filename = page + '.jpg';
                a.href = url;
                a.download = filename;
                a.click();
                window.URL.revokeObjectURL(url);
                if(pageCurrent<pageTotal){
                    pageCurrent++;
                    downloadPic(pageCurrent);
                }
            })), 9000)

不行,还是下得飞起
11#
发表于 2022-3-6 21:38:30 | 只看该作者
  1. new Promise((resolve, reject) => {
  2.     setTimeout(() => {
  3.         var a = document.createElement('a');
  4.         var url = window.URL.createObjectURL(blob);
  5.         var filename = page + '.jpg';
  6.         a.href = url;
  7.         a.download = filename;
  8.         a.click();
  9.         resolve()
  10.     }, 1000);
  11. }).then(_ => {
  12.     window.URL.revokeObjectURL(url);
  13.     if(pageCurrent<pageTotal){
  14.         pageCurrent++;
  15.         downloadPic(pageCurrent);
  16.     }
  17. })

复制代码


替换fetch(picUrl).then(res => res.blob().then(blob => { 里的试下,能不能用我也不知道,因为我也是菜鸟
10#
 楼主| 发表于 2022-3-6 21:33:21 | 只看该作者
靓坤 发表于 2022-3-6 21:24
用settimeout把fetch那里包住即可

好的。我试下,这个fetch套得太多
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2026-1-15 06:10 , Processed in 0.064612 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表