-
Notifications
You must be signed in to change notification settings - Fork 0
/
interval_cancellation_real.js
50 lines (41 loc) · 1.99 KB
/
interval_cancellation_real.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const fs = require('fs');
// Define the cancellable function for real-time updates
var cancellable = function(fn, args, t) {
// Call fn immediately with the provided arguments
fn.apply(null, args);
// Set up an interval to call fn repeatedly every t milliseconds
const timer = setInterval(() => fn.apply(null, args), t);
// Define cancelFn function to clear the interval
const cancelFn = () => clearInterval(timer);
// Return the cancelFn function
return cancelFn;
};
// Define the function to read stock data from a JSON file
const readStockDataFromFile = () => {
console.log('Reading stock data from file...'); // Log each time the function is called
// Read stock data from the JSON file (replace with actual file path)
const stockData = fs.readFileSync('stocks.json', 'utf8');
return JSON.parse(stockData);
};
// Define the function to update the dashboard with new stock data
const updateDashboard = (stockData) => {
// Update the dashboard UI with the latest stock data
console.log('Dashboard updated with new stock data:', stockData);
// Update dashboard UI (replace with actual UI update code)
};
// // Define the function to update the stocks.json file with new data
// const updateStockDataToFile = (newStockData) => {
// // Convert the new stock data to JSON format
// const jsonData = JSON.stringify(newStockData, null, 2);
// // Append the JSON data to the stocks.json file
// fs.appendFileSync('stocks.json', jsonData);
// console.log('New stock data appended to stocks.json file.');
// };
// Usage example:
// Start real-time updates for the dashboard
const cancelRealTimeUpdates = cancellable(readStockDataFromFile, [], 5000); // Update interval: 5000ms (5 seconds)
// Simulate closing the dashboard after 20 seconds
setTimeout(() => {
cancelRealTimeUpdates(); // Cancel real-time updates
console.log('Dashboard closed or inactive. Real-time updates stopped.');
}, 20000); // Simulate closing the dashboard after 20 seconds