Created
October 11, 2019 18:44
-
-
Save snxraven/3bf345772b825d378db602f1e2bd9de1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Usage: | |
* | |
* coolerInterval( func, interval, triggerOnceEvery); | |
* | |
* - func : the function to trigger | |
* - interval : interval that will adjust itself overtime checking the clock time | |
* - triggerOnceEvery : trigger your function once after X adjustments (default to 1) | |
* - This script uses fs.stat to check the last modified time for a file | |
* - stats.mtime gives us the last modified time in miliseconds | |
* - We compare current time, date1 - and now, date 2 getting the time difference | |
* - we then use jsonfile from npm to write our new json data after the difference is | |
* - 1800 seconds (30 Minutes) or older. This script runs every Minute | |
* - Change runtime frequency via line 21 by chaning 1 to any timeframe 1 = 1 Minute | |
*/ | |
var coolerInterval = function(func, interval, triggerOnceEvery) { | |
// grab the starting time set next tick and count to 0 | |
var startTime = new Date().getTime(), | |
nextTick = startTime, | |
count = 0; | |
// Set the Trigger timeframe - Onece Every Minute | |
triggerOnceEvery = triggerOnceEvery || 1; | |
// set up interval function | |
var internalInterval = function() { | |
// Adds a value and the variable and assigns the result to that variable. | |
nextTick += interval; | |
// add one (+1) | |
count++; | |
// If count matches line 21 fun the function and reset the count | |
if (count == triggerOnceEvery) { | |
func(); | |
count = 0; | |
} | |
// set the interval tick | |
setTimeout(internalInterval, nextTick - new Date().getTime()); | |
}; | |
// run the function | |
internalInterval(); | |
}; | |
// Run the function and code the file checks | |
coolerInterval(function() { | |
console.log("Its been 60 Seconds"); | |
// we require fs to access the filesystem | |
var fs = require('fs'); | |
// define now | |
const now = new Date(); | |
// use fs.stat to look up json modified date | |
fs.stat("/home/user/file.json", function(err, stats) { | |
// Grab your modified time variable and set it | |
var mtime = stats.mtime; | |
// set date 1 | |
var date1 = mtime | |
// set date 2 | |
var date2 = now | |
// time difference in miliseconds | |
var timeDiff = Math.abs(date2.getTime() - date1.getTime()); | |
// time difference in seconds | |
var timeDiffInSecond = Math.ceil(timeDiff / 1000); | |
// Check if the time difference is 30 Minutes or Over | |
if (timeDiffInSecond > "1800") { | |
// Log the call to edit | |
console.log("Its been 30 Minutes, clearing PWD! For shell 1") | |
// Require jsonfile npm install jsonfile | |
const jsonfile = require('jsonfile') | |
// define our file | |
const file = '/home/user/file.json' | |
// present our data | |
const obj = { | |
data: 'data to write' | |
} | |
// write the data! | |
jsonfile.writeFile(file, obj, function(err) { | |
if (err) console.error(err) | |
}) | |
} | |
}); | |
}, 1000, 60); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment