簡體   English   中英

NodeJS如果字符串包含執行腳本

[英]NodeJS if string contains execute script

我有以下腳本:

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);

我只想在字符串包含“ Alert:found!”的情況下執行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!' );
});

救命!

您是在問如何將兩者結合嗎?

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM