js检测资源是否存在

  this.getPicture(() => {
            console.log(‘成功’)
          },url);

// 获取图片
    getPicture(conBack,url) {
      let that = this;
          // 使用示例
          this.checkResource(url).then(exists => {
            if (exists) {
              conBack()
            } else {
              if(that.timerPic) {
                clearTimeout(that.timerPic)
                that.timerPic = null
              }
              that.timerPic = setTimeout(() => {
                that.getPicture(conBack,url)
              },5000)
            }
          });
    },
    checkResource(url) {
      return fetch(url, { method: 'HEAD' })
        .then(response => response.ok)
        .catch(error => console.error('Resource check failed:', error));
    },

 

posted @ 2024-06-06 10:47  黑白棋学弟  阅读(5)  评论(0编辑  收藏  举报