NodeJS,如果string包含执行脚本

时间:2017-06-30 21:36:25

标签: javascript node.js

我有以下脚本:

function execute(){
require("fs").readFile("sometextfile.txt", function(err, cont) {
    if (err)
        throw err;
    console.log("ALERT:"+(cont.indexOf("mystring")>-1 ? " " : " not ")+"found!");
});

 }

 setInterval(execute,9000);

我想只在字符串包含"警告:找到时才执行javascript!"

剧本:

var Prowl = require('node-prowl');

var prowl = new Prowl('API');

prowl.push('ALERT', 'ALERT2', function( err, remaining ){
        if( err ) throw err;
        console.log( 'I have ' + remaining + ' calls to the api during current hour. BOOM!' );
});

帮助!

1 个答案:

答案 0 :(得分:0)

您是否在考虑如何将两者合并?

const fs = require('fs');
const Prowl = require('node-prowl');

const prowl = new Prowl('API');

function alert() {
    prowl.push('ALERT', 'ALERT2', function(err, remaining) {
        if (err) throw err;
        console.log('I have ' + remaining + ' calls to the API during current hour. BOOM!');
    });
}

function poll() {
    fs.readFile('sometextfile.txt', function(err, cont) {
        if (err) throw err;
        if (cont.indexOf('mystring') !== -1) {
            alert();
        }
    });
}

setInterval(poll, 9000);