当我点击youtube嵌入iframe时,我正在尝试显示或开始计数。 这是我的代码
<iframe id="startcount" width="900" height="400" src="https://www.youtube.com/embed/qAM8wEHPccU" frameborder="0" allowfullscreen></iframe>
<span align="center" style="color:#0091bf; font-weight:bold; font-size:24px;">You will be redirected in
<span id="counter">30</span> second(s) for credit verification. </span>
<script type="text/javascript">
$(document).ready(
function() {
$("#startcount").click(function() {
function countdown() {
var i = document.getElementById('counter');
if (parseInt(i.innerHTML)<=0) {
//location.href = 'earn_credit_ac.php?ads_id=<?php echo $del_id; ?>';
}
i.innerHTML = parseInt(i.innerHTML)-1;
}
setInterval(function(){ countdown(); },1000);
});
});
</script>
帮助我,我是jquery的新手。
感谢All
答案 0 :(得分:0)
如果您倒计时,则应检查该值是否大于0且不小于0。 以下代码应该有效:
$(document).ready(function() {
$("#startcount").click(function() {
function countdown() {
var i = $("#counter");
var value = parseInt(i.html());
var interval;
if (value >= 0) { // I think you want "greater equal" (>=) instead of "less equal" (<=)
//location.href = 'earn_credit_ac.php?ads_id=<?php echo $del_id; ?>';
}
else {
clearInterval(interval); // stop interval if counted to -1
}
i.html(value - 1);
}
setInterval(countdown, 1000);
});
});
答案 1 :(得分:0)
我正在使用其他方法来完成此任务。
Html:
<button id="play-button" class="btn mobile-full" style="padding:10px;"> Play </button>
<iframe id="video" src="//www.youtube.com/embed/NV16WtEXP6E?autohide=2&border=0&wmode=opaque&enablejsapi=1&modestbranding=1&controls=0&showinfo=1&rel=0" frameborder="0" allowfullscreen></iframe>
<div align="center">
<span align="center" style="color:#0091bf; font-weight:bold; font-size:24px;">You will be redirected in
<span id="counter"><?php echo $vid_duration; ?></span> second(s) for credit verification. </span>
</div>
Css:
.button {
width: 48px;
height: 48px;
cursor: pointer;
&:hover {
fill: white;
}
}
.defs {
position: absolute;
top: -9999px;
left: -9999px;
}
iframe {
float: left;
width: 300px;
height: 200px;
}
.buttons {
padding: 1rem;
background: #f06d06;
float: left;
}
JavaScript:
<script>
// https://developers.google.com/youtube/iframe_api_reference
// global variable for the player
var player;
// this function gets called when API is ready to use
function onYouTubePlayerAPIReady() {
// create the global player from the specific iframe (#video)
player = new YT.Player('video', {
events: {
// call this function when player is ready to use
'onReady': onPlayerReady
}
});
}
function onPlayerReady(event) {
// bind events
var playButton = document.getElementById("play-button");
playButton.addEventListener("click", function() {
player.playVideo();
function countdown() {
var i = document.getElementById('counter');
if (parseInt(i.innerHTML)<=0) {
location.href = 'earn_credit_ac.php?ads_id=<?php echo $del_id; ?>';
}
i.innerHTML = parseInt(i.innerHTML)-1;
}
setInterval(function(){ countdown(); },1000);
});
}
// Inject YouTube API script
var tag = document.createElement('script');
tag.src = "//www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
</script>
这是在手机上播放YouTube视频的唯一方法,同时启动时间计数器。
感谢所有人。