Prevent repeated Ajax requests

Solution: function anti-shake + disabled button after user click + function throttling

  • 1, use the anti – shake function to limit the first click in a row within a certain period of time is valid.
  • 2, if the background request processing is very slow, the background is processing after the first valid request is sent, until the second valid request is not finished, then the background may error, solution, the first request after the click button disabled, until the request response to enable.
  • 3, if the background processing quickly, click the is effective and timely treatment, but it does not need so many requests (such as query request, is actually a result, it is not necessary to repeat the request, may be the user has no intention of multipoint, may also be a user boring deliberately multipoint, it is possible that the malicious attacks), then he took a request time interval, For example, 5s(different values can be set for different requests).
  • 4, therefore, the final set time and background processing return time of a larger value can be.

closure

  • Closures are javascript’s way of supporting first-class functions, which are expressions that can refer to their internal scope variable (the variable first declared in the scope), which can be assigned to a variable, passed as an argument to a function, or returned as a function return value.
  • A closure is a stack frame that is allocated at the beginning of a function execution and is not released when the function returns at the end of execution (just as a stack frame is allocated in the heap instead of the stack!).
  • Closure applications:
    • For example, when writing a Currization function, use closures to store parameters in memory:
    var currying = function(fun) {/ / formatting the arguments var args = Array. The prototype. Slice. The call (the arguments, 1);return function() {/ / collection of all the parameters in the same Array, to calculate the var _args = args. The concat (Array) prototype. Slice. The call (the arguments));return fun.apply(null, _args);
        };
    }
    Copy the code
    • Simulate private variables or private methods:
    const people = (num) => {
        var num = num;
        return {
    	increase: () => {
    	    num++;
            },
            get: () => {
                return num;
            }
        }
    }
    const man = people(4);
    man.increase();
    man.get();
    Copy the code
    • Avoid quoting errors:
    for (var i = 0; i < 4; i++) {
        (function(_i) {
        	setTimeout(function() {
      		    console.log(_i)
    	    }, 1000)
        })(i)
    }
    Copy the code

New keyword procedure

  • Creates an empty object, and the this variable references the object and inherits the prototype of the function.
  • Properties and methods are added to the object referenced by this.
  • The newly created object is referred to by this and implicitly returns this.

The pseudocode is as follows:

var obj = {}; obj.__proto__ = Foo.prototype; //Foo is the constructor foo.call (obj); retun obj;Copy the code

TCP vs UDP

  • TCP connection-oriented (Ensure that there is a three-way handshake to create a connection before transmission.)
  • UDP is not connection-oriented. It does not establish a connection with the peer before transmitting data, and does not send an acknowledgement signal to the received data. The sender does not know whether the data will be correctly received and does not need to resend the data.
  • UDP transmission rate is higher, because there is no need to confirm sending and receiving data, better real-time
  • TCP data is ordered. The data sent in the same order is received in the same order. UDP is unordered, sent (1,2,3), and possibly received in (1,3,2) order. Applications must do their own grouping sorting.

TCP does not lose packets

TCP uses serial number confirmation and timer to detect packet loss and ensure data order. Use checksums to detect errors in packet segments.

  • 1. The sender sends the first TCP packet segment containing 1 (variable serial number) and 1460 bytes of data to the receiver. The receiver replies with a TCP segment with no data (including only the header), using the confirmation number 1461 to indicate that it has fully received and requests the next segment.
  • 2. The sender then sends a second TCP segment containing 1461 and 1460 bytes of data to the receiver. In normal cases, the receiver replies with a TCP segment with no data and uses the confirmation number 2921 (1461+1460) to indicate that the next segment has been fully received and requested. Send and receive and so on.
  • 3. However, when these packets are all connected, there is no need for the receiver to respond every time. For example, if he receives TCP segments 1 through 5, he only needs to respond to the fifth segment. In the example, TCP segment 3 is missing, so he can only respond to TCP segment 2, although he receives TCP segment 4 and 5.
  • 4. The sender did not receive a response after sending the third item, so he re-sent the third item when the timer expired. (Each time the sender sends a TCP segment, the clock is started again: RTT).
  • 5. This time the third item has been successfully received, the receiver can directly confirm the fifth item, because 4 and 5 items have been received.