ralphie
ralphie

Reputation: 132

Meteorjs Timer based event trigger

I made an application where I generated a file and will be downloaded in the client desktop. Right now, I manage to create this using manual click event. Here's my code.

Template.productionRun.events({
    'click .btGenFile': function(e,t){

        console.log('i\'m now click')
        // bootbox.alert('Welcome back New Meteorite!');
        var strLoc = "C:/LabelPrint/labelPrint.lbl";
        var strQty = 1;
        var str1 = "14N1234";
        var str2 = 50;
        var str3 = "Made in Philippines";
        var str4 = "Cebu";


        var strPj = strLoc + "\r\n" + 
        strQty + "\r\n" +
        str1 + "\r\n" +
        str2 + "\r\n" +
        str3 + "\r\n" +
        str4;
        var blob = new Blob([strPj], {
            type: "text/plain;charset=utf-8;",
        });
        saveAs(blob, "job.pj");
    }
});

However, the frequency of the file generation will be at least 1 file/sec, so I need it to automatically download the file without the user. Is there a way I can use a timer to trigger the download event?

I saw the meteor-reactive-timer package but sadly I don't have any idea how to use it my code.

Upvotes: 1

Views: 108

Answers (1)

Jim O'Brien
Jim O'Brien

Reputation: 2552

Rather than using your template's events hook, use the created or rendered hook, with setInterval:

Template.productionRun.rendered = function() {
    // This code runs once the template has been rendered
    console.log('Template has been rendered!');

    var interval = 1000;

    setInterval(function() {
        // This runs once every `interval` milliseconds
        var strPj = [
            "C:/LabelPrint/labelPrint.lbl",
            1, "14N1234", 50,
            "Made in Philippines",
            "Cebu"
        ].join('\r\n');

        var blob = new Blob([strPj], {
            type: "text/plain;charset=utf-8;",
        });
        saveAs(blob, "job.pj");
     }, interval);
};

Note that I've also used Array.join to construct strPj instead of individual strings. This is a lot faster and easier to maintain!

Upvotes: 1

Related Questions