本文整理汇总了TypeScript中timers.setInterval函数的典型用法代码示例。如果您正苦于以下问题:TypeScript setInterval函数的具体用法?TypeScript setInterval怎么用?TypeScript setInterval使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了setInterval函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: initEventListeners
export var hostWebSocket = (ws: any, req: express.Request) => {
// The code runs when the client first initiates the connection. All initialization code for the WebSocket must go here:
var chartId: string = req.params.chart;
// Initialize the event listeners that send messages to the client in response to changes to the hosted ValueChart.
var eventListeners: any[] = initEventListeners(chartId, ws);
hostConnections.set(chartId, { chartId: chartId, connectionStatus: 'open'});
// Send message confirming successful connection:
ws.send(JSON.stringify({ data: 'complete', chartId: chartId, type: MessageType.ConnectionInit }));
// Register a timer to send a KeepConnection message to the client every five seconds. These messages a are hack used to keep
// the websocket open. It seems that the websocket will silently close if messages are continuously being exchanged between the server and client.
var connectionTimer = timers.setInterval(() => {
ws.send(JSON.stringify({ type: MessageType.KeepConnection, chartId: chartId, data: 'Keep connection Open' }));
}, 5000);
// Register an event handler to handle message events. This handler will be called whenever the client sends a message to
// the WebSocket.
ws.on('message', (msg: string) => {
var hostMessage: HostMessage = JSON.parse(msg);
// Handle the different messages types:
switch (hostMessage.type) {
case MessageType.ConnectionInit:
// Do nothing. The client should not send ConnectionInit messages.
break;
default:
// Do nothing. The message is not of a known type.
break;
// The client has replied to the KeepConnection message sent by the server. Nothing needs to be done about this.
// The client will reply to every KeepConnection message.
case MessageType.KeepConnection:
// Do nothing.
break;
}
});
// Register an event handler to handle the WebSocket close event. This event is fired when the client closes the WebSocket connection
// with the back-end.
ws.on('close', () => {
timers.clearInterval(connectionTimer);
// Cleanup the event listeners. They need to be removed so that the back-end does not try to send messages to the client via a closed WebSocket.
eventListeners.forEach((listener: any) => {
hostEventEmitter.removeListener(listener.eventName, listener.listener);
});
});
};
示例2: listening
function listening() {
console.log("api listening on *:" + settings.api.listen_port)
timers.setInterval(function() {
progress_report();
server.timer.reset();
}, settings.api.progress_report_timer)
}
示例3: setInterval
const xs = new TestObservable<number>(obs => {
let count = 0;
const interval = setInterval(() => {
obs.next(count++);
if (count === 3) {
clearInterval(interval);
obs.complete();
}
}, 10);
return new EmptySubscription();
});
示例4: Subscriptions
server.on("connection", (websocket: WebSocket): void => {
const subscriptions = new Subscriptions(augurEmitter);
const pingInterval = setInterval(() => safePing(websocket), webSocketConfigs.pingMs || 12000);
websocket.on("message", (data: WebSocket.Data): void => {
let message: any;
try {
message = JSON.parse(data as string, addressFormatReviver);
if (!isJsonRpcRequest(message)) return logger.error("bad json rpc message received:", message);
} catch (exc) {
return safeSend(websocket, makeJsonRpcError("-1", JsonRpcErrorCode.ParseError, "Bad JSON RPC Message Received", { originalText: data as string }));
}
try {
if (message.method === "subscribe") {
const eventName: string = message.params.shift();
try {
const subscription: string = subscriptions.subscribe(eventName, message.params, (data: {}): void => {
safeSend(websocket, makeJsonRpcResponse(null, { subscription, result: data }));
});
safeSend(websocket, makeJsonRpcResponse(message.id, { subscription }));
} catch (exc) {
safeSend(websocket, makeJsonRpcError(message.id, JsonRpcErrorCode.MethodNotFound, exc.toString(), false));
}
} else if (message.method === "unsubscribe") {
const subscription: string = message.params.shift();
subscriptions.unsubscribe(subscription);
safeSend(websocket, makeJsonRpcResponse(message.id, true));
} else {
dispatchJsonRpcRequest(db, message as JsonRpcRequest, augur).then((result): void => {
safeSend(websocket, makeJsonRpcResponse(message.id, result || null));
}).catch((err) => {
logger.error("getter error: ", err);
safeSend(websocket, makeJsonRpcError(message.id, JsonRpcErrorCode.InvalidParams, err.message, false));
});
}
} catch (exc) {
safeSend(websocket, makeJsonRpcError(message.id, JsonRpcErrorCode.ServerError, exc.toString(), exc));
}
});
websocket.on("close", () => {
clearInterval(pingInterval);
subscriptions.removeAllListeners();
controlEmitter.emit(ControlMessageType.WebsocketClose);
});
websocket.on("error", (err) => {
logger.error(err);
controlEmitter.emit(ControlMessageType.WebsocketError, err);
});
});
示例5: testPromisify
});
}
}
/////////////////////////////////////////////////////
/// Timers tests : https://nodejs.org/api/timers.html
/////////////////////////////////////////////////////
{
{
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, "");
示例6: broadcast
const wss = new WebSocket.Server({port: 3000});
function broadcast(data: string) {
// Iterate over all clients
wss.clients.forEach(client => {
// Send if connection is open
if (client.readyState === WebSocket.OPEN) client.send(data);
});
}
let i = 10;
let direction = 1;
setInterval(() => {
if (i < 25) {
i = 25;
direction = 1;
} else if (i > 85) {
i = 85;
direction = -1;
} else {
i += 5 * direction;
}
const message: SiloFillMessage = {
messageType: 1,
deviceId: 42,
value: i
};
broadcast(JSON.stringify(message));
}, 250);
示例7:
Debug.scripts().forEach(function(script: any) { console.log(script.name); });
}
}
/////////////////////////////////////////////////////
/// Timers tests : https://nodejs.org/api/timers.html
/////////////////////////////////////////////////////
namespace timers_tests {
{
let immediateId = timers.setImmediate(function(){ console.log("immediate"); });
timers.clearImmediate(immediateId);
}
{
let counter = 0;
let timeout = timers.setInterval(function(){ console.log("interval"); }, 20);
timeout.unref();
timeout.ref();
timers.clearInterval(timeout);
}
{
let counter = 0;
let timeout = timers.setTimeout(function(){ console.log("timeout"); }, 20);
timeout.unref();
timeout.ref();
timers.clearTimeout(timeout);
}
}
/////////////////////////////////////////////////////////
/// Errors Tests : https://nodejs.org/api/errors.html ///