本文整理汇总了TypeScript中timers.setTimeout函数的典型用法代码示例。如果您正苦于以下问题:TypeScript setTimeout函数的具体用法?TypeScript setTimeout怎么用?TypeScript setTimeout使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了setTimeout函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: searchLessons
export function searchLessons(req: Request, res: Response) {
const queryParams = req.query;
const courseId = queryParams.courseId,
filter = queryParams.filter || '',
sortOrder = queryParams.sortOrder || 'asc',
pageNumber = parseInt(queryParams.pageNumber) || 0,
pageSize = parseInt(queryParams.pageSize) || 3;
let lessons = Object.values(LESSONS).filter(lesson => lesson.courseId == courseId).sort((l1, l2) => l1.id - l2.id);
if (filter) {
lessons = lessons.filter(lesson => lesson.description.trim().toLowerCase().search(filter.toLowerCase()) >= 0);
}
if (sortOrder == "desc") {
lessons = lessons.reverse();
}
const initialPos = pageNumber * pageSize;
const lessonsPage = lessons.slice(initialPos, initialPos + pageSize);
setTimeout(() => {
res.status(200).json({payload: lessonsPage});
},1000);
}
示例2: runLoader
public runLoader(nameLoader) {
this.loaderService.registeredLoaders[nameLoader].activate();
let that = this;
setTimeout(() => {
that.loaderService.registeredLoaders[nameLoader].deactivate();
}, 2000);
}
示例3: testChangeHashEvent
public testChangeHashEvent() {
let windowBuilder = this.getWindowBuilder();
let window = windowBuilder.setLocation("http://example.com").getMock();
let callsCounter = this.createCallsCounter();
window.addEventListener("hashchange", () => {
callsCounter.call();
});
window.location.hash = "#foo1";
window.location.hash = "#foo2";
window.dispatchEvent(new HashChangeEvent("hashchange"));
setTimeout(() => {
this.assertSame(3, callsCounter.getCounter());
}, 100);
}
示例4: function
return function() {
let now = +new Date()
if (!previous) previous = now
if (now - previous > atleast) {
fn()
// 重置上一次开始时间为本次结束时间
previous = now
} else {
clearTimeout(timer)
timer = setTimeout(function() {
fn()
}, delay)
}
}
示例5: interval
protected async interval() {
clearTimeout(this.timeouts.betsInterval);
try {
const _modifiedAt = await global.db.engine.findOne(global.systems.bets.collection.data, { key: 'betsModifiedTime' });
if (this.modifiedAt !== _modifiedAt) {
this.modifiedAt = _modifiedAt;
this.currentBet = await global.db.engine.findOne(global.systems.bets.collection.data, { key: 'bets' });
this.bets = await global.db.engine.find(global.systems.bets.collection.users);
}
} catch (e) {
global.log.error(e.stack);
} finally {
this.timeouts.betsInterval = setTimeout(() => this.interval(), 1000);
}
}
示例6: registerBridge
registerBridge("setTimeout", (rt: Runtime, bridge: Bridge, fn: ivm.Reference<() => void>, timeout: number) => {
const t = setTimeout(() => {
try {
fn.applyIgnored(null, [])
} catch (e) {
// ignore
}
try {
fn.release()
} catch (e) {
// ignore
}
}, timeout)
t.unref()
return Promise.resolve(new ivm.Reference(t))
})
示例7: searchLessons
export function searchLessons(req: Request, res: Response) {
console.log('Searching for lessons ...');
// const error = (Math.random() >= 0.5);
// if (error) {
// console.log("ERROR loading lessons!");
// res.status(500).json({message: 'random error occurred.'});
// }
// else {
const queryParams = req.query;
const courseId = queryParams.courseId,
filter = queryParams.filter || '',
sortOrder = queryParams.sortOrder,
pageNumber = parseInt(queryParams.pageNumber) || 0,
pageSize = parseInt(queryParams.pageSize);
let lessons = Object.values(LESSONS).filter(lesson => lesson.courseId == courseId).sort((l1, l2) => l1.id - l2.id);
if (filter) {
lessons = lessons.filter(lesson => lesson.description.trim().toLowerCase().search(filter.toLowerCase()) >= 0);
}
if (sortOrder == "desc") {
lessons = lessons.reverse();
}
const initialPos = pageNumber * pageSize;
const lessonsPage = lessons.slice(initialPos, initialPos + pageSize);
setTimeout(() => {
res.status(200).json({payload: lessonsPage});
},1000);
// }
}
示例8: tryRead
function tryRead() {
attempts += 1
try {
const chunk = info.stream.read(1024 * 1024)
log.debug("chunk is null? arraybuffer?", !chunk, chunk instanceof Buffer)
if (chunk) {
info.readLength += Buffer.byteLength(chunk)
}
if (!chunk && !info.endedAt && attempts < 10) {
// no chunk, not ended, attemptable
setTimeout(tryRead, 20 * attempts)
return
}
if (!chunk && attempts >= 10) {
cb.applyIgnored(null, [null, null])
} else if (chunk instanceof Buffer) {
// got a buffer
cb.applyIgnored(null, [null, transferInto(chunk)])
}
// got something else
else {
cb.applyIgnored(null, [null, chunk])
}
try {
cb.release()
} catch (e) {
// ignore
}
} catch (e) {
cb.applyIgnored(null, [e.message])
try {
cb.release()
} catch (e) {
// ignore
}
}
}
示例9: saveCourse
export function saveCourse(req: Request, res: Response) {
const id = req.params["id"],
changes = req.body;
console.log("Saving course", id, JSON.stringify(changes));
COURSES[id] = {
...COURSES[id],
...changes
};
setTimeout(() => {
res.status(200).json(COURSES[id]);
}, 2000);
}
示例10: testPromisify
{
{
const immediateId = timers.setImmediate(() => { console.log("immediate"); });
timers.clearImmediate(immediateId);
}
{
const counter = 0;
const timeout = timers.setInterval(() => { console.log("interval"); }, 20);
timeout.unref();
timeout.ref();
timers.clearInterval(timeout);
}
{
const counter = 0;
const timeout = timers.setTimeout(() => { console.log("timeout"); }, 20);
timeout.unref();
timeout.ref();
timers.clearTimeout(timeout);
}
async function testPromisify() {
const setTimeout = util.promisify(timers.setTimeout);
let v: void = await setTimeout(100); // tslint:disable-line no-void-expression void-return
let s: string = await setTimeout(100, "");
const setImmediate = util.promisify(timers.setImmediate);
v = await setImmediate(); // tslint:disable-line no-void-expression
s = await setImmediate("");
}
}