How to display loading status with preloader and multiple images?

前端 未结 1 1964
陌清茗
陌清茗 2021-01-01 07:11

I am building a slideshow with a few hundred images and would like to build a nice loading bar, so the idea was to preload the images using JavaScript, then initialize the r

1条回答
  •  耶瑟儿~
    2021-01-01 07:43

    Try using the function from my answer to this question:

    function incrementallyProcess(workerCallback, data, chunkSize, timeout, completionCallback) {
      var itemIndex = 0;
      (function() {
        var remainingDataLength = (data.length - itemIndex);
        var currentChunkSize = (remainingDataLength >= chunkSize) ? chunkSize : remainingDataLength;
        if(itemIndex < data.length) {
          while(currentChunkSize--) {
            workerCallback(data[itemIndex++]);
          }
          setTimeout(arguments.callee, timeout);
        } else if(completionCallback) {
          completionCallback();
        }
      })();
    }
    
    
    // here we are using the above function to take 
    // a short break every time we load an image
    function init() {
      incrementallyProcess(function(element) {
        imageBuf[element] = new Image();
        imageBuf[element].onload = function(){count()};
        imageBuf[element].src = "thumbs/"+element;
      }, imgList, 1, 250, function() { 
        alert("done loading"); 
      });
    }
    

    You may want to modify the chunk size parameter as well as the length of the timeout to get it to behave just like you want it to. I am not 100% sure this will work for you, but it is worth a try...

    0 讨论(0)
提交回复
热议问题