jplayer ajax使用timeupdate运行多次

问题描述 投票:1回答:1

我正在尝试将currentTime,duration和currentPrecentAbsolute值发送到php页面,该页面将捕获这些变量并将其使用ajax存储到数据库中。我在暂停事件上能正常工作,但是当我尝试使用timeupdate时,我接到多个电话。

我希望能够在currentPercentAbsolute达到25%,50%和75%时自动发送数据。我知道问题是什么,我不确定如何解决。时间更新速度在不同的浏览器中是可变的,我相信默认值大约为250ms。因此,尽管我的百分比是25%,50%和75%,但ajax仍将继续运行。大约4次(每秒4 * 250ms)。当然,只有视频的时长大约为100秒。如果它是200秒长,那么currentPercentAbsolute将以这些百分比停留一整秒,运行我的ajax 8次。

所以最终我的问题是,做我正在尝试的更好的方法是什么?附带一提,我还想实现

$(window).unload(function(){my ajax call in here});

为了在用户关闭窗口而不完成视频的情况下获取相同的统计信息。我有它的工作,但遇到了同样的问题。它在timeupdate的每次迭代中运行。任何帮助都将不胜感激。

我的时间更新代码

$(window).unload(function(){my ajax call in here});
php ajax jplayer
1个回答
0
投票

我解决了多次运行。我声明了3个全局变量,然后做了

timeupdate: function (event) { var totaltime = Math.floor(event.jPlayer.status.duration); var currenttime = Math.floor(event.jPlayer.status.currentTime); var percent = Math.floor(event.jPlayer.status.currentPercentAbsolute); if(percent == '25'){ $('#timeNow').html(currenttime); $('#percentNow').html(percent); $.ajax({ type: 'POST', url: 'teststats.php', data : {timeWatched : currenttime, percentWatched : percent, totalTime : totaltime}, success : function(data) { } }); } if(percent == '50'){ $('#timeNow').html(currenttime); $('#percentNow').html(percent); $.ajax({ type: 'POST', url: 'teststats.php', data : {timeWatched : currenttime, percentWatched : percent, totalTime : totaltime}, success : function(data) { } }); } if(percent == '75'){ $('#timeNow').html(currenttime); $('#percentNow').html(percent); $.ajax({ type: 'POST', url: 'teststats.php', data : { timeWatched : currenttime, percentWatched : percent, totalTime : totaltime}, success : function(data) { } }); } },

和设置

if((percent == '25') && (!a)){

在if语句中。

仍然遇到问题

a = true;

[也许它正在做应该做的事情。所有3个变量(currentime,百分比和总时间)均以0开头。当我尝试关闭浏览器窗口或导航到另一个URL时,我不确定jplayer是否从timeupdate清除值。有人对此有任何想法吗?谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.