本文介绍了我的JSPerf测试用例“中断",在评估带回调的toggle()时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在测试我的答案对,我创建了相比要慢多少.令我惊讶的是,报告说该脚本经过了额外的处理,可以更快地执行!除了结果违反直觉之外,我还怀疑有问题,因为在返回结果很久之后,我还在屏幕上看到了切换.

While testing my answer to Adding HTML5 hidden attribute support to jQuery .toggle() method, I created a JSPerf test to determine how much slower .toggle(function() \[...\]) would be compared to .toggle(). To my amazement, the script with additional processing is reported to execute faster! Besides the results being counter-intuitive, I suspect a problem because I also see the toggling on the screen long after the results have returned.

如何修正"测试以获取准确的结果?

推荐答案

$('button').click(function() {
    $('#myElement').toggle(function() { alert("called") })
})

传递给toggle的回调函数仅在切换动作完成后 才执行.换句话说,这是一个异步操作,因此,如果要测量触发回调之前所花费的时间,请使用jsPerf的异步/延迟功能.

The callback function you pass to toggle only gets executed after the toggle action completes. In other words, it’s an asynchronous action, so if you want to measure how long it takes until the callback is fired, use jsPerf’s async/defer feature.

这篇关于我的JSPerf测试用例“中断",在评估带回调的toggle()时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 19:24