Comrades, help, please. I am not familiar with JS and jQuery, but I already sat down with them. Unfortunately, they gave me only a week for the assignments - please help. Here are two tasks:

one). We write most client javascript using jQuery. In order to maximize this, we try to use the capabilities of the framework as little as possible, including little-known ones.

Task. You need to make a plugin that allows you to use, in the standard way for jQuery, the event “click with the left mouse button only”.

2). We pay great attention to the performance of our applications in general. It is not rare that a single function (for example, an event handler) runs long enough, but is called more often than it would be sufficient for the user qualities of the entire system.

The task is about one of the ways to fix the situation. You need to implement a common solution (using the decorator pattern), which allows you to call any function no more than N times per second, even if the call to this function occurs more often.

Thank.

  • Interestingly, did they ask you this during the interview? - yozh
  • No, this is the first task - the interview was not yet ...) - Boby

3 answers 3

First:

$(selector).click(function(){...}); 

Second (without decorator):

 var checker = true; var time = 1000;//Задержка function someFunction(){ if(checker){ //do something checker = false; window.setTimeout(function(){ checker = true;}, time);//возвели таймер } } 

I'm not strong in js and jQuery, it will be interesting to look at the implementation with the decorator!

  • MEGArespect ... thank you very much ...) Only correctly, I realized that the decorator can be “somehow standardized” screwed?) - Boby
  • Yes, you can standard, [here is the link] [1], you need throttling. Now I’m digging around with it myself, but something doesn’t want it to work normally ... [1]: plugins.jquery.com/project/debouncing-throttling - metazet
  • Ok ... thanks ...) - Boby

JQuery is not just good, it also has excellent documentation .

With the second question, I do not know what to say, the braking mechanism of the event, do you need something? I somehow badly imagine this.

  • No, this TSom described is possible, then simply it will be necessary to "put" all the functions into a class that will already resolve, the task is, to put it mildly, non-trivial and I’m not going to write for myself personally :) And I described the possible algorithm - Zowie
  • Sorry ... I’d better look at the code ...) Thanks for the tips ...) - Boby
  • put the same - deamondz

one.

$ (document) .bind ("click", function (event) {if (event.which == 1) alert ("mouse left!")})