Enable Jquery Only after Javascript recursive function is done executing -


//image sequence functions //preload array /**  * preload images in directory.  * @param {string} baseurl   * @param {string} extension  * @return {promise} resolve array<image>.  */ function preloadimages(baseurl, extension, starter) {     return new promise(function (res) {         var = starter;         var images = [];         // inner promise handler         var handler = function (resolve, reject) {             var img = new image;             var source = baseurl + + '.' + extension;             img.onload = function () {                 i++;                 resolve(img);             }             img.onerror = function () {                 reject('rejected after ' + + 'frames.');             }             img.src = source;         }         // once catch inner promise resolve outer one.         var _catch = function () {             res(images)         }         var operate = function (value) {             if (value) images.push(value);             // inner recursive promises chain.             // stop catch resolving outer promise.             new promise(handler).then(operate).catch(_catch);         }         operate();     }) }  /**  * draw array of images on canvas.  * @param {canvas} canvas   * @param {array<image>} imagelist   * @param {number} refreshrate   */ function play(canvas, imagelist, refreshrate, framewidth, frameheight, percentage) {     // since we're using promises, let's promisify animation too.     return new promise(function (resolve) {         // may need adjust framerate         // requestanimationframe 60/120 fps depending on browser         // , refresh rate of display devices.         var ctx = canvas.getcontext('2d');         var ts, = 0,             delay = 1000 / refreshrate;         var roll = function (timestamp) {             if (!ts || timestamp - ts >= delay) {                 // since image prefetched need specify rect.                 ctx.drawimage(imagelist[i], 0, 0, framewidth, frameheight);                 i++;                 ts = timestamp;             }             if (i < imagelist.length * percentage)                 requestanimationframe(roll);             else                 resolve(i);         }         roll();     }) } //combine preload , play 1 function preloadandplay(id, actor, event, percentage) {     var preload = preloadimages('/static/videos/' + actor + '/clip_' + event + '/', 'png', 1);     preload.then(function (value) {         //console.log('starting play');         //canvas html5 object id         var canvas = document.getelementbyid(id);         play(canvas, value, 30, 960, 540, percentage) // ~30fps             .then(function (frame) {                 console.log('roll finished after ' + frame + ' frames.')             })     }); }       $.when(preloadandplay('myimage', actor, event, percentage)).done(function(){         $('.data').click(function () {             if ($(this).is(':checked')) {                 $(this).removeclass('hidden');                 $(that).addclass('hidden');             }             = this;         });         $('.data').change(function () {             if (('input[name=b]:checked').length) {                 $('#trial_next').prop('disabled', false);                 cbchecked = document.queryselector('[name="b"]:checked').value;                 debug(cbchecked);             }         });     }); 

i .change , .click jquery enabled after preloadandplay javascript function done executing. jquery suppose allow radio buttons can selected. preloadandplay function loads series of images displays them html5 canvas seems video/animation. see above preloadandplay function uses promise , recursion. code works it's don't want people make selection before video ends. above code tried.

there several issue @ code @ question. @ preloadimages function parameter @ function passed promise constructor named res, though resolve() called within function @ <img> load event, though resolve undefined within function. handler not called, not reaching call undefined resolve(). operate function within promise constructor not necessary. no value returned operate, see why value undefined @ .then() chained promise?.

you can use promise.all() pass iterable object chained .then() should called when of values or promise values passed fulfilled.

not expected effect of play() call is, nor portion of code mentioned being part of original inquiry. adjusted code @ staccksnippets call requestanimationframe each image.

const baseurl = "https://lorempixel.com/50/50/";    const data = ["cats", "nature", "city"];    const canvas = document.queryselector("canvas");    function preloadimages(image) {    return new promise(function(resolve) {      var img = new image;      var source = baseurl + image;      img.onload = function() {        resolve(img);      }      img.onerror = function() {        reject('rejected after ' + + 'frames.');      }      img.src = source;    })  }    function play(canvas, imagelist, refreshrate, framewidth, frameheight, percentage) {    // since we're using promises, let's promisify animation too.    return new promise(function(resolve) {      // may need adjust framerate      // requestanimationframe 60/120 fps depending on browser      // , refresh rate of display devices.      var ctx = canvas.getcontext('2d');      var ts, = 0,        delay = 1000 / refreshrate;      var roll = function(timestamp) {        if (!ts || timestamp - ts >= delay) {          // since image prefetched need specify rect.          ctx.drawimage(imagelist[i], 0, 0, framewidth, frameheight);          i++;          ts = timestamp;        }        if (i < imagelist.length /* * percentage */ )          requestanimationframe(roll);        else          resolve(i);      }      roll();    })  }    function preloadandplay(percentage) {    var preload = promise.all(data.map(preloadimages))      .then(function(images) {        console.log("preloadimages complete, images:", images);        images.foreach(function(img) {          document.body.appendchild(img)        });        return images      })      .catch(function(err) {        console.error(err);        throw err;      });    return preload.then(function(value) {      console.log("preload complete, value:", value);      return play(canvas, value, 30, 960, 540, percentage) // ~30fps        .then(function(frame) {          console.log('roll finished after ' + frame + ' frames.');          return "done";        })    });  }    preloadandplay()  .then(function(data) {    console.log(data);  })    .catch(function(err) {      console.error(err)    })
<canvas></canvas>


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -