排除重复的模值

发布于 2024-12-06 01:19:09 字数 875 浏览 0 评论 0原文

我触发了一个事件,显示视频中的进度:

_currentPosition = 3.86 秒 _currentPosition = 4.02 秒 _currentPosition = 4.16 秒 。

我想做的是每五秒向服务器发送一次通知,以指示进度 我可以使用 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;

谢谢。

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

_currentPosition = 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疑心病 2024-12-13 01:19:09

看来你已经快到了。我只是将 _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;
    }
}

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;
    }
}
青瓷清茶倾城歌 2024-12-13 01:19:09
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");
  }
}
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");
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文