Search code examples
node.jsgulpgulp-watch

How to timeout gulp-watch if inactive


I have a gulp watch task, and I want it to stop automatically if there has been no changes in the last hour. Is this possible?


Solution

  • Use timeouts:

    gulp.task('watcher', function() {
        var watcher = gulp.watch('./app/*.js', ['jshint']);
        var timeout = setTimeout(watcher.end, 60*60*1000);
    
        watcher.on('change', function() {
            clearTimeout(timeout);
            timeout = setTimeout(watcher.end, 60*60*1000);
        });
    });
    

    Everytime something in your Glob changes, you kill the timeout and start it anew. Otherwise you end your watcher.