javascript - execute a method on an existing object with window.setInterval -


Is it possible to run the method on an existing object at window time .setInterval method i set some global variable and By calling the method of this global variable in the interval, I can imitate it, but I wanted to know that it is possible using the method directly.

Best regards, Keshav

Yes, you can do it. You need an assistant function to create your existing object "bound" new function:

  var someRandomObject = {someMethod: function () {//. . whatever   },   // ... }; // This "bind" function is a "toy" version of the bind (object, method) {return function () {method.call (object); }; } Var interval = set interlabel (bind (some rand object, somewhatand object somemath), 1000);  

Now when the interval timer calls your method ("some method"), "this" indicator will reference the object.

That version of "Tie" is simplified. Libraries such as prototype, functional, jquery etc. typically provide a more robust version. Additionally, the "bind" function will be the original part of javascript & mdash; It's already in some browsers.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -