开发者

Exclude repeated modulus values

开发者 https://www.devze.com 2023-04-06 23:15 出处:网络
I have an event firing that shows progress in a video: _c开发者_开发知识库urrentPosition = 3.86 seconds

I have an event firing that shows progress in a video:

_c开发者_开发知识库urrentPosition = 3.86 seconds _currentPosition = 4.02 seconds _currentPosition = 4.16 seconds etc.

What I'm trying to do is to send a notification to the server every five seconds, to indicate progress. I can round the seconds down to nearest integer using Math.floor. Then I can use modulus to get every fifth second. What I'm not seeing is how not to send a repeat for (e.g.) 5, since 5.02, 5.15, 5.36 etc. will all qualify.

I want something like the following, which is executing in a quickly-firing event, similar to enterframe. But I'm not sure how or where to do the test for _sentNumber, where to declare it, where to reset it...

    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;

Thanks.


It looks like you're almost there. I'd just put the _sentNumber declaration inside the if statement:

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;
    }
}


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");
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消