排除重复的模值
我触发了一个事件,显示视频中的进度:
_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来你已经快到了。我只是将 _sentNumber 声明放在 if 语句中:
It looks like you're almost there. I'd just put the _sentNumber declaration inside the if statement: