0

I have a web3 event watcher set up in a Javascript file as such:

var a;
contract.events.allEvents(function(error, event){
   if (event.event == "Set") {
        a = event.returnValues['val'];
   }
});

When I run this file with node eventWatcher.js it will loop indefinitely and run the code within my conditionals every time an event occurs.

However, I want to keep a timer concurrently such that when a certain amount of time passes, I can unset the value of a. Is this possible?

Thanks!

closed as off-topic by user19510, Vignesh Karthikeyan, Achala Dissanayake, Raghav Sood, Henk Aug 10 '18 at 12:37

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Ethereum, the decentralized application platform and smart contract enabled blockchain, within the scope defined in the help center." – user19510, Vignesh Karthikeyan, Achala Dissanayake, Raghav Sood, Henk
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    normally in JavaScript a setTimer() function is used. Isn't this function working for you? Correct me if I am wrong, but this question is more about Javascript and not Ethereum itself. – Nulik Aug 10 '18 at 0:18
0

To set a time event you can just use a normal JavaScript timer.

var a;
contract.events.allEvents(function(error, event){
    if (event.event == "Set") {
        a = event.returnValues['val'];
        window.setTimeout(function() {
            a = '';
        }, (60 * 1000)); // clear a in 60 seconds
    }
});

If you only want to do this once on the timer rather than once whenever the event occurs, you may prefer to put the timer on a loop with setInterval and use a get inside the timer function to grab events, and display the most recent.

Not the answer you're looking for? Browse other questions tagged or ask your own question.