Issue with setInterval function?

Added by Jordan Scango over 5 years ago

I'm currently working on a plugin for uTorrent, that'll let you manage, search, and stream any of your torrents (you can even stream downloading torrents this way, as long as the contents aren't rar'd). It'll also give you notifications for newly added or completed torrents, but that's where I'm having trouble.

This is how I'm doing it now:

interval = setInterval(notificationService, (settings.refresh_interval * 1000));

And this works fine if it runs during the plugin's initial startup, when the feature is enabled at startup. But if I run this after the inital startup, as a result of the user changing a setting, it runs the notificationService once, but never again after that. And I can't figure out why, this is the only javascript I've ever wrote so I need some help.

The full plugin is attached if you want to test. Everything should be working, aside from the fact that the plugin needs to be restarted to start the notification service. So if anything other than that is wrong let me know.

Here's just the code: http://pastebin.com/dLmkDesK

I have 2 booleans in settings, and when they're changed or initialized, the notificationServiceHandler function runs, which determines whether to start the service, end it, or do nothing. But like I said, it only actually runs more than once when it's called from the init, otherwise if you change the value later it only runs once.

So I think this has to be some kind of context issue, but I don't what. The only potentially relevant thing I found while googling was that if the window object closes/reloads, setInterval gets reset. I don't know how that relates to Movian though, could it be a bug?

I'm totally at a loss here, any help would be greatly appreciated. And again if you test the plugin, let me know if there's anything wrong or if you think anything is missing.

utorrent.zip (47.2 KB)


Replies (5)

RE: Issue with setInterval function? - Added by Leonid Protasov over 5 years ago

Jordan Scango wrote:

I'm currently working on a plugin for uTorrent, that'll let you manage, search, and stream any of your torrents (you can even stream downloading torrents this way, as long as the contents aren't rar'd). It'll also give you notifications for newly added or completed torrents, but that's where I'm having trouble.

This is how I'm doing it now:
[...]

And this works fine if it runs during the plugin's initial startup, when the feature is enabled at startup. But if I run this after the inital startup, as a result of the user changing a setting, it runs the notificationService once, but never again after that. And I can't figure out why, this is the only javascript I've ever wrote so I need some help.

The full plugin is attached if you want to test. Everything should be working, aside from the fact that the plugin needs to be restarted to start the notification service. So if anything other than that is wrong let me know.

Here's just the code: http://pastebin.com/dLmkDesK

I have 2 booleans in settings, and when they're changed or initialized, the notificationServiceHandler function runs, which determines whether to start the service, end it, or do nothing. But like I said, it only actually runs more than once when it's called from the init, otherwise if you change the value later it only runs once.

So I think this has to be some kind of context issue, but I don't what. The only potentially relevant thing I found while googling was that if the window object closes/reloads, setInterval gets reset. I don't know how that relates to Movian though, could it be a bug?

I'm totally at a loss here, any help would be greatly appreciated. And again if you test the plugin, let me know if there's anything wrong or if you think anything is missing.

Well, when your plugin loads it calls:

settings.createBool('notifications', "On new torrents",false, function(v) {
        service.notifications = v;
        notificationServiceHandler(v);
    });

By default v is false i suppose and this in fact calls:
 function notificationServiceHandler(enable) {
        if (enable & interval_enabled == false) {
            showtime.trace("Start Notification Service at rate " +  settings.refresh_interval);
            interval_enabled = true;
            interval = setInterval(notificationService, (settings.refresh_interval * 1000));
        }
        else if (service.completed_notifications == false & service.notifications == false) {
            if (interval_enabled) {
                showtime.trace("Disable Notification Service - " + interval);
                clearInterval(interval);
                cid = 0;
                interval_enabled = false;
            }
        }

RE: Issue with setInterval function? - Added by Jordan Scango over 5 years ago

Right, by default it's false because the notifier could be resource intensive, so it seems better opt-in. But once you set either notification setting to true it runs that again, but then it does get to the setInterval line, and it does run it, just only once. But then if you reload the plugin with that variable being true it runs it indefinitely like it should.

The fact that it's only running once when I call it outside of the initial load is what I just don't get.

RE: Issue with setInterval function? - Added by Andreas Smas over 5 years ago

It's a bug in Movian caused by mixing of repeating timers (your code) and one shot timers (used for settings)

Fix coming up...

RE: Issue with setInterval function? - Added by Andreas Smas over 5 years ago

Fix is in 4.9.524

Remember to crank up the minimum version required in plugin.json to match this

    "showtimeVersion": "4.8",

^ that thing...

RE: Issue with setInterval function? - Added by Jordan Scango over 5 years ago

Awesome, I'll be sure to do that.

(1-5/5)