如何从second
获取videoid:uoSDF234
值并将其倒计时?
首先,我需要"videoid":"uoSDF234"
作为第一个倒计时,然后下一个将是"videoid":"0apq3ss"
,依此类推(如果我添加更多数据)。
当我单击停止按钮时,label id="vstopresult"
将显示已停止的videoid
和second
。videoid
中的每个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>
最佳答案
一个粗略的可能性是使用setInterval
,例如。
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>
关于javascript - 选择对象内部的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27814803/