javascript - How to ignore requestAnimationFrame in browsers which don't support it -


Assume that we have a feature / module, which increases a website so it is not really necessary and it I use the requestAnimationFrame , which is like IE8 / 9, I could have polyphil requestAnimationFrame , but because it is only an increase, the old browser should ignore it. The code of this module looks more or less like this:

 ; {Function (window, document, undefined) {'strict use'; // example module // constructor function module, option) {// ...} Module.prototype = {init: function () {// if requestAnimationFrame Not supported, a warning warning ("init") should be popped up;}, method: function () {/ if the request animationfreck is not supported, then alert any warning ("Start");}}; Window.Module = Module;} (window, document) should pop up;   

Then I can create a new example

  var example = new module (document.getElementById ('test'));   

and "interact with"

  instance.init (); Instance.method ();   

The problem with this code is that there is an error in IE9 because IE9 does not know the new "function" like "code" requestAnimationFrame . I can simply add a if statement

  (if.re window.requestAnimationFrame) {var instance = new module (document.getElementById ('test')) ; }   

And I use it everywhere. However in the module it would be easier to check the requestAnimationFrame support at one time. If this is not supported then nothing should be done, the old browser should ignore it. So I tried to do something

  // @constructor function module (L, option) {if (! Window.requestAnimationFrame) {return false; } // ...}   

but it prevents old browsers from executing all methods such as ".init ()" or ".method ()" (in this case) Do I really want to give it a if statement in every code or can I do anything?

I'm not sure that when you already understood the argument, Choose to apply in the form. Just change the comments with the code and you can go:

  Module.prototype = {init: function () {if (requestof requestAnimationFrame === 'undefined') return; Warning ("init"); }};   

Or check window.requestAnimationFrame if you wish.


Additional Answer:

How to avoid writing multiple codes, ifs

If we can use the statement, as always in javascript, we use the function to implement new 'syntax':

  function restricted ) {Return function () {if (requestof requestAnimationFrame === 'undefined') return; Return f.apply (this, logic); }}   

You can now restrict ( instead of :

  Module.prototype = {init: restricted (function () {alert ("init");}), method: restricted (function () {warning ("start");})};  < / Pre>  

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -