播放视频时javascript递增setInteval计数器
我有以下代码来检测用户是否空闲.在页面加载时,计时器将运行,如果用户空闲了特定的秒数,则计时器将暂停,如果用户处于活动状态,则计时器将恢复.我也有检测视频是否正在播放的代码,如果视频正在播放,计时器应该运行,如果视频停止/暂停,超时将运行并检测用户是否仍处于活动状态
问题是,即使正在播放视频,计时器也会暂停,并开始空闲.我想要的是当视频播放时,计时器应该计时到递增.
以下是我的代码:
选择 | 换行 | 行号
- function setPlayingVideoToTrue(){playing_video = true;}
- function setPlayingVideoToFalse(){playing_video = false;}
- // check if a video iframe exists
- var iframe_videos = $('body').find('iframe');
- if(iframe_videos.length > 0){
- // create ready events for every iframe
- iframe_videos.each(function(index){
- // add a temporary id for the iframe
- // append additional parameters to the end of the iframe's src
- var temporary_player_id = 'iframe_player_'+ index;
- var new_iframe_src = $(this).attr('src') +'?api=1&player_id='+ temporary_player_id;
- $(this).attr('id', temporary_player_id);
- $(this).attr('src', new_iframe_src);
- // add event listener for ready
- $f(this).addEvent('ready', iframe_ready);
- });
- // when a player is ready, add event listeners for play, pause, finish, and playProgress
- function iframe_ready(player_id) {
- $f(player_id).addEvent('play', setPlayingVideoToTrue);
- $f(player_id).addEvent('playProgress', setPlayingVideoToTrue);
- $f(player_id).addEvent('pause', setPlayingVideoToFalse);
- $f(player_id).addEvent('finish', setPlayingVideoToFalse);
- }
- }
- function start_idle_timer(){
- var timer = 0;
- function increment_duration()
- {
- if(isPaused === false)
- {
- timer++;
- }
- // increment timer if video is playing
- if(playing_video === true){
- clearTimeout(idleTimer);
- isPaused = false;
- }
- if(playing_video === false && isPaused === false){
- // stop timer if the user is idle for 3 minutes
- var idleTimer = setTimeout(function(){
- // console.log('idle');
- clearInterval(check_time);
- isPaused = true;
- // a modal will apper to inform that user is on idle state
- $('#linkage').trigger('click');
- var modal_timer = 0;
- // timer for modal idle timer
- continue_modal_timer = setInterval(function(){
- modal_timer++;
- inactivity_timer = modal_timer;
- if(modal_timer == 60)
- {
- clearInterval(continue_modal_timer);
- clearTimeout(idleTimer);
- }
- }, 1000)
- }, 10000);
- }
- // bind to all elements on DOM possible events indicating the user is active
- $('*').bind('mousemove click mouseup mousedown keydown keypress keyup submit change mouseenter scroll resize dblclick', function () {
- clearTimeout(idleTimer);
- isPaused = false;
- });
- }
- // initialize the interval
- check_time = setInterval(increment_duration, 1000);
- }
- // check if start_timer is present from the loading page to record the time duration of the user
- if($('.start_timer').length > 0){
- start_idle_timer();
- }