1

I had written a callback function to capture the snapshot of running video using html5 video control and canvas.

I used a for loop to iterate and call the same callback function take the burst capture.

If i add alert('') in the callback , the video in the background rerendering when alert message display, the burst snap shot works fine as taking diff photos(frames/images of the running video). But when I removed the alert('') , the video does not run in the background and the bursted images are the same instead of different.

The code

for (var i = 0; i < burstcount; i++) {
        var wcam = Webcam;
            wcam.burst_snap(function (dataurl, id) {
                var arrayindex = passedName + "_" + id;
                imgid = imgid + i;
                alert(dataurl);
                burstcapturedata[arrayindex] = dataurl;
            }, i);
            var j = 0;
            while (j < 10000000000) {
                j++;
            }
    }
    DisplayBurstedImages();

}

Susindar
  • 7
  • 5
  • 2
    Sounds like you have an asynchronous call and you are treating it as a synchronous one. But without code, I can not verify that. – epascarello Apr 22 '15 at 13:14
  • @epascarello - code added , can u have a look at it, – Susindar Apr 22 '15 at 13:27
  • Looks like burst_snap is asynchronous...Not sure what `burst_snap` is... And that while loop is bad practice. – epascarello Apr 22 '15 at 13:30
  • @epascarello - i removed the while loop , its only for making the current execution to hold a bit, i used sleep(milliseconds) , one of javascript guy's idea to hold an application for a period of time. do i need to post the burst_snap code as well – Susindar Apr 22 '15 at 13:46
  • What is Webcam and burst_snap? Is it triggered a set number of times? When is it done? – epascarello Apr 22 '15 at 13:46
  • its triggered from a button onClick and upto the burstcount is reached where burstcount is the count to take snaps continuosly.The Webcam is a library provided by a guy to capture webcam snapshots.its an singleton object. the burst_snap is the method that called inside the Webcam library – Susindar Apr 22 '15 at 13:53
  • So how many images are you waiting for before you really want to call DisplayBurstedImages? – epascarello Apr 22 '15 at 13:55
  • around 3 , but i think of upto six. – Susindar Apr 22 '15 at 13:56
  • Is that value in `burstcount`?? – epascarello Apr 22 '15 at 13:56
  • yes , its the burst limit , currently i gave it to 3. can be given upto 6 though – Susindar Apr 22 '15 at 13:57

3 Answers3

1

Yes, actually. Alert holds next execution of code.
If your code works with alert it means that you require delay.
Try setTimeout or put the code in the correct place where everything is getting loaded.

piggy
  • 365
  • 3
  • 19
syms
  • 413
  • 2
  • 12
0

I guess it needs time for binding video. you can use setTimeout function for delay.

var delay =100;

setTimeout(function () {/*your function*/ }, delay);

delay = delay + 300;
jcubic
  • 61,973
  • 54
  • 229
  • 402
Learner
  • 59
  • 1
  • 7
  • i tried it just now cos lot of guys suggested that , no use .. i cant figure out the reason yet, the alert somehow do the magic here, i need some seriuos explaination abt this – Susindar Apr 22 '15 at 13:42
0

Your code needs to look something like this:

 var wcam = Webcam;
 var cnt = 0;
 wcam.burst_snap(function(dataurl, id) {
   var arrayindex = passedName + "_" + id;  //you do not have an array if this is the key
   imgid = imgid + cnt;  //no clue what this is for
   cnt++;
   burstcapturedata[arrayindex] = dataurl;
   if (cnt===burstcount) {
     DisplayBurstedImages();
   }
 }, 100);  //no clue what that value is supposed to be
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • i tried the code , but it getting called only once not up to the burstcount(meant 3). i have not used the for loop , should i use for loop too? – Susindar Apr 22 '15 at 14:11
  • Does burst_snap only call one time or does it work multiple times? If I have an api it would be easy to help. Without it, I am just guessing what this stuff does. – epascarello Apr 22 '15 at 14:12
  • the burst_snap getting called only one time . i am not using any loops , should i use for loop. the Webcamjs is the api i am using for capturing images https://github.com/jhuckaby/webcamjs i copied and pasted the snap function of the api as burst_snap for my understanding. – Susindar Apr 22 '15 at 14:15