1

我使用下面的函数来更新和调用drawAccel();构建动画条形图的函数。

function messagecb(header, message) {
    if(header.type == 6) {
       // processEchoReply(message);

   } else if(header.type == 4) {
        // accel
        var accels = message.b64UnpackAccelMsg();

        for(var index = 0; index < accels.length; ++index) {
            var accel = accels[index];
            var totalClock = accelEpochAdjust(accel.clock);

            addAccelDatum(totalClock, accel.x,  accel.y, accel.z);

        }

    if ( typeof messagecb.counter == 'undefined' ) {
      messagecb.counter = 0;
    }

    ++messagecb.counter;
    if (messagecb.counter % 10 === 0) {
      drawAccel();
    }

    } else if(header.type == 3) {
       // info
       var info2 = message.b64UnpackInfo2Msg();

       displayCurrentPosition(info2.fixtime, info2.lat, info2.lon, info2.alt);
       displayMobileStatus(info2.rssi, info2.bandClass, info2.batt);

    } else if(header.type == 11) {
        btReceive(header, message);
}

}

不过,我在 IE8 中遇到了一些间歇性的性能问题。所以我想收集在 update for 循环中运行的经过的挂墙时间,而不是调用drawAccel()渲染器,除非我使用的挂墙时间少于 50%。

伪代码示例:

  if ((lastEnteredTime - lastExitedTime)/(currentTime - lastEnteredTime) < .5){
       drawAccel();
  } else {
   //do nothing 
  }

我的问题是我不确定如何获取循环的最后输入时间和最后退出时间,以便我可以运行此条件。有任何想法吗?谢谢!

4

1 回答 1

1

我不清楚你到底想做什么,但这样的事情应该让你接近。 +new Date()会给你自 1970 年 1 月 1 日以来的毫秒数,所以在不同的地方进行相同的调用应该能够得到你想要的

var start = +new Date();

for(var index = 0; index < accels.length; ++index) {
    var accel = accels[index];
    var totalClock = accelEpochAdjust(accel.clock);

    var current = +new Date();
    var timeElapsedInMs = current - start;
    //not sure the exact test you want to run here

    addAccelDatum(totalClock, accel.x,  accel.y, accel.z);
}

根据您的评论进行编辑。所以如果你总是想要一个 lastEntered 和 lastExited 值,这样的东西可能就是你想要的

var lastEntered, lastExisted = +new Date();

for(var index = 0; index < accels.length; ++index) {
    lastEntered = +new Date();

    var accel = accels[index];
    var totalClock = accelEpochAdjust(accel.clock);

    var timeElapsedInMs = current - start;
    //not sure the exact test you want to run here

    addAccelDatum(totalClock, accel.x,  accel.y, accel.z);

    lastExisted = +new Date();
}

从那里你可以做任何你需要的比较。

于 2013-06-02T04:34:15.557 回答