How to subscribe and unsubscribe the window load event in Rxjs

I know every web programmers are aware of adding window.addeventlistener to ensure some codes are executing once when loading the web page. You can remove the listener by removing using removeEventListener to make avoid the memory leak.

You can do the same thing in a RxJs fashion using take(1). So you no need to unsubscribe it explicitly to avoid the memory leak.
var onWindowLoad=Observable.fromEvent(window,'load');

var onLoadSubscription=onWindowLoad.take(1).forEach(function(event){
alert("onLoadFireEvent");
});

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s