我有一个被调用次数未知的函数。我需要知道函数运行了多少次,所以我在做:
(function () {
var i = 0,
increment = function () {
if (i === 0) {
setTimeout(function () {
console.log('increment was called ' + i + ' times.'); // increment was called 3 times.
i = 0;
}, 0);
}
i++;
};
increment();
increment();
increment();
})();
谁能告诉我这在所有浏览器中是否可靠,或者是否有更好的模式来实现这一点?