本文整理汇总了TypeScript中backburner.throttle函数的典型用法代码示例。如果您正苦于以下问题:TypeScript throttle函数的具体用法?TypeScript throttle怎么用?TypeScript throttle使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了throttle函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: function
QUnit.test('throttle without a target, with args, not immediate - can be canceled', function(assert) {
let done = assert.async();
let bb = new Backburner(['batman']);
let calledCount = 0;
let calledWith: object[] = [];
function throttled(first) {
calledCount++;
calledWith.push(first);
}
let foo = { isFoo: true };
let bar = { isBar: true };
let baz = { isBaz: true };
bb.throttle(throttled, foo, 10, false);
bb.throttle(throttled, bar, 10, false);
let timer = bb.throttle(throttled, baz, 10, false);
assert.equal(calledCount, 0, 'throttle method was not called immediately');
setTimeout(() => {
assert.deepEqual(calledWith, [], 'throttle method has not been called on next tick');
bb.cancel(timer);
}, 0);
setTimeout(() => {
assert.deepEqual(calledWith, [], 'throttle method is not called when canceled');
done();
}, 20);
});
示例2: resetError
bb.run(function() {
bb.throttle(target1, method, arg1, 1000, false);
bb.throttle(target2, method, arg1, arg2, 1000, false);
debugInfo = bb.getDebugInfo();
resetError();
assert.deepEqual(debugInfo.timers,
[
{
args: [arg1],
method,
stack: oneStack,
target: target1
},
{
args: [arg1, arg2],
method,
stack: twoStack,
target: target2
}
]
, 'debugInfo is output');
});
示例3: setTimeout
setTimeout(() => {
assert.ok(!wasCalled, 'attempt to call throttle again didn\'t happen');
throttle = bb.throttle(null, throttler, 40);
assert.ok(wasCalled, 'newly inserted throttle after timeout functioned');
assert.ok(bb.cancel(throttle), 'wait time of throttle was cancelled');
wasCalled = false;
throttle2 = bb.throttle(null, throttler, 40);
assert.notEqual(throttle, throttle2, 'the throttle is different');
assert.ok(wasCalled, 'throttle was inserted and run immediately after cancel');
done();
}, 60);
示例4: function
QUnit.test('cancelTimers', function(assert) {
assert.expect(8);
let done = assert.async();
let bb = new Backburner(['one']);
let laterWasCalled = false;
let debounceWasCalled = false;
let throttleWasCalled = false;
let timer1 = bb.later(() => laterWasCalled = true, 0);
let timer2 = bb.debounce(() => debounceWasCalled = true, 0);
let timer3 = bb.throttle(() => throttleWasCalled = true, 0, false);
assert.ok(timer1, 'Timer object was returned');
assert.ok(timer2, 'Timer object was returned');
assert.ok(timer3, 'Timer object was returned');
assert.ok(bb.hasTimers(), 'bb has scheduled timer');
bb.cancelTimers();
setTimeout(function() {
assert.ok(!bb.hasTimers(), 'bb has no scheduled timer');
assert.ok(!laterWasCalled, 'later function was not called');
assert.ok(!debounceWasCalled, 'debounce function was not called');
assert.ok(!throttleWasCalled, 'throttle function was not called');
done();
}, 100);
});