2011-09-22 58 views
0

我有一个事件触发,显示在影片进度:排除重复模量值

_currentPosition =3.86秒 _currentPosition =4.02秒 _currentPosition =4.16秒 等

什么我想做的是每5秒向服务器发送一个通知,以指示进度。我可以使用Math.floor将秒数舍入到最接近的整数。然后我可以使用模数来获得每五秒。我没有看到的是如何不发送重复(例如)5,因为5.02,5.15,5.36等将全部合格。

我想要类似于以下内容的东西,它在快速启动事件中执行,类似于enterframe。但我不知道如何或在哪里做测试_sentNumber,这里声明一下,在那里将其复位......

var _sentNumber:int; 
    //_currentPosition is the same as current time, i.e. 5.01, 5.15, etc. 
var _floorPosition:int = Math.floor(_currentPosition); //returns 5 
    if(_floorPosition % 5 == 0) //every five seconds, but this should only happen 
             // once for each multiple of 5. 
    { 
     if(_floorPosition != _sentNumber)  //something like this? 
     { 
     sendVariablesToServer("video_progress"); 
     } 
    _sentNumber = _floorPosition; 

感谢。

回答

0

它看起来像你几乎在那里。我只是把_sentNumber声明if语句里面:

var _sentNumber:int = 0; 

private function onUpdate(...args:Array):void // or whatever your method signature is that handles the update 
{ 
    //_currentPosition is the same as current time, i.e. 5.01, 5.15, etc. 
    var _floorPosition:int = Math.floor(_currentPosition); //returns 5 
    if(_floorPosition % 5 == 0 && _floorPosition != _sentNumber) //every five seconds, but this should only happen once for each multiple of 5. 
    { 
     sendVariablesToServer("video_progress"); 
     _sentNumber = _floorPosition; 
    } 
} 
+0

是的,就是这样。谢谢! – David

+0

为什么*这个工作? int是一个原始数据类型。因此,说var _sentNumber:int就像说var _sentNumber:int = new int()。所以每次它通过_sentNumber应该重新初始化为0,所以这段代码应该失败。 – David

+0

我以为你只是为了简单起见而把var _sentNumber:int放在那个位置!是的,您应该将该声明移出该函数的范围,以便它不会被重新初始化。我会更新我的答案以反映这一点。 – meddlingwithfire

0
private var storedTime:Number = 0; 

private function testTime():void{ 
    //_currentPosition is the same as current time, i.e. 5.01, 5.15, etc. 
    if(_currentPosition-5 > storedTime){ 
    storedTime = _currentPosition 
    sendVariablesToServer("video_progress"); 
    } 
}