0

How can I get the second value from videoid:uoSDF234and make it countdown ?

  1. first I need "videoid":"uoSDF234" be the first countdown then next will be "videoid":"0apq3ss" and so on (if I add more data).
  2. when I click the stop button, label id="vstopresult" will show the stopped videoid and second.
  3. the countdown will looping for each videoid in videolist.

var videoinfo = [{"startdatetime":"2014-12-21 00:23:14","totalsecondrun":"2019402310","videolist":
[{"videoid":"uoSDF234","second":"10"},{"videoid":"0apq3ss","second":"14"}]}]; 
// JavaScript Document

var calduration = function(){
  $.each(videoinfo, function(i, obj) {
   $("#vstart").append(obj.startdatetime);
   $("#vtotoals").append(obj.totalsecondrun);
   
   $("#vid").append(videoinfo[0].videolist[0].videoid);
   $("#vlefts").append(videoinfo[0].videolist[0].second);

     var output = $("#vlefts");
     var isPaused = false;
     var time = videoinfo[0].videolist[0].second;
     var t = window.setInterval(function() {
      if(!isPaused) {
       time--;
       output.text(time);
      }
      if (time == 0){
       clearInterval(t);
      }
     }, 1000);
     
     //with jquery
     $("#vpause").on('click', function(e) {
      e.preventDefault();
      isPaused = true;
     });
     
     $("#vplay").on('click', function(e) {
      e.preventDefault();
      isPaused = false;
     });
     
     $("#vstop").on('click', function(e) {
      clearInterval(t);
      $("#vstopresult").append(time);
     });
    
  });
               

};
  
  
calduration();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div>
         <label id="vstart"></label><br>
         <label id="vtotoals"></label><br>
            <label id="vid"></label><br>
            <label id="vlefts"></label><br>
            <input type="button" value="play" id="vplay"/>
            <input type="button" value="pause" id="vpause"/>
            <input type="button" value="stop" id="vstop"/><br>
            <label id="vstopresult"></label>
        </div>
FeelRightz
  • 2,777
  • 2
  • 38
  • 73
  • 1
    So what is your expected output? What does this algorithm do? Be more specific – Vincent Beltman Jan 07 '15 at 08:05
  • @VincentBeltman I have updated the real expected I want. – FeelRightz Jan 07 '15 at 08:12
  • @VincentBeltman my expected output is when I stop it show me the `videoid` and `second` , for this something like player, I have updated question what really I need – FeelRightz Jan 07 '15 at 08:19
  • To get the `second` value of `videoid:uoSDF234`, the reference would be `videoinfo[0].videolist[0].second`. – Xotic750 Jan 07 '15 at 08:19
  • Did I correctly interpret your question in that you want videoid `0apq3ss` to be displayed **after** the `192312` seconds of videoid `uoSDF234` have elapsed? Because right now you walk through all the entries of `"videolists"` with `$.each` without waiting for one video to end before you go to the next. If I run the code without the `var timer=[..]` part I get both videoid's immediately. – asontu Jan 07 '15 at 08:23
  • @Xotic750 do you have any idea how to continue countdown to the next `videoid` and `second` ? example when `videoid:uoSDF234` `second is 0`, `"videoid":"0apq3ss"` will continue countdown – FeelRightz Jan 07 '15 at 08:27
  • @funkwurm hi, I agree with your first question. Now I updated my code, first run is `uoSDF234` , but how to make it auto change to next `videoid` and `second` when `uoSDF234`(first video) is count to 0 ? – FeelRightz Jan 07 '15 at 08:35
  • Yes. Like @funkwurm said, you need to wait for the first countdown to finish before moving onto the next. Right now you start the first and then immediately go to the second without waiting for the first timer to complete. So you need to rethink your method. – Xotic750 Jan 07 '15 at 08:41
  • @Xotic750 I not say immediately go to the second, I have mention when the first is count to 0. – FeelRightz Jan 07 '15 at 08:45
  • I'm sorry but you are. You get the first lot of data and you create your counters and update the DOM, then you immediately go to the second data and create your counters and then you overwrite the DOM with the new information. Don't believe me, put an `alert` in as the first line of your `each` function. – Xotic750 Jan 07 '15 at 08:51
  • When @Xotic750 says "you [..] immediately go to the second", he means that's what the code is doing right now, and the reason of that is that you're using `$.each` to go through all the entries of `"videolist"`. What you wanna do is only look at `.videolist[0]`, do what it needs to do, setInterval, etc. and when that is all done, then go to `.videolist[1]`. – asontu Jan 07 '15 at 09:01
  • @funkwurm I agree what you said, but I only know `$.each` to read json data , by the way i updated my code – FeelRightz Jan 07 '15 at 09:08

1 Answers1

1

One crude possibility is to use setInterval, an example may be.

var videoinfo = [{
        "startdatetime": "2014-12-21 00:23:14",
        "totalsecondrun": "2019402310",
        "videolist": [{
            "videoid": "uoSDF234",
            "second": "10"
        }, {
            "videoid": "0apq3ss",
            "second": "14"
        }]
    }],
    index = 0,
    timerEl = document.getElementById('timer'),
    countDown = 0,
    intervalId;

function startCountdown() {
    if (index < videoinfo[0].videolist.length) {
        countDown = videoinfo[0].videolist[index].second;
        intervalId = setInterval(function () {
            timerEl.textContent = countDown;
            if (countDown < 1) {
                clearInterval(intervalId);
                index += 1;
                setTimeout(function () {
                    startCountdown();
                }, 1000);
            } else {
                countDown -= 1;
            }
        }, 1000);
    }
}

startCountdown();
<div id='timer'></div>
Xotic750
  • 22,914
  • 8
  • 57
  • 79
  • I have use `setInterval` – FeelRightz Jan 07 '15 at 09:49
  • I have see each number have countdown once, but in mycode , second is json , so how to countdown each one continuously? – FeelRightz Jan 07 '15 at 09:52
  • You have an `array`, just like I have in my example and the property `second` of each item (an `object`) has a number value. – Xotic750 Jan 07 '15 at 09:53
  • can you try to mix my json ? sorry because I still very new in jquery hard to understand – FeelRightz Jan 07 '15 at 09:59
  • I haven't used any jQuery (which is just a library of functions written in javascript with a focus on cross browser DOM manipulation) and it seems you need to go and read about javascript. In particular [`array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) and [`object`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object). Your [`JSON`](http://en.wikipedia.org/wiki/JSON) (an open standard format that uses human-readable text to transmit data objects) has beed parsed and is now just arrays and objects. – Xotic750 Jan 07 '15 at 10:03
  • 1
    There is plenty of reading here on SO and out there on the internet, but you can try reading this as a start. http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json – Xotic750 Jan 07 '15 at 10:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68382/discussion-between-im-happy-and-xotic750). – FeelRightz Jan 08 '15 at 02:32
  • I have an idea , how to turn videoid value and second value to 2 array? then I can use your method example ... I try to change it to array but failed [click here for js fiddle](http://jsfiddle.net/dkqLk55a/) – FeelRightz Jan 08 '15 at 02:44
  • when the last index is countdown finish can return to index 0 again ? – FeelRightz Jan 08 '15 at 04:02
  • You can do that very simply. – Xotic750 Jan 08 '15 at 10:24