The outrageous JS run speed

Source: Internet
Author: User

First, a piece of code

var function  (URL, func, type) {    varnew  Date (). GetTime (),        = URL + (url.indexof ('? ') + 1 ? ' & ': '? ') + ' callback= ' + callback,        = createlement ("script", {"src": url});     // Console.log (Callback, "callback")    function (data) {        func (data, type);    };    Document.body.appendChild (s);}

This is a function of a JSONP request, where type is required for work, please ignore. It's okay to use the feeling, but I actually have a bug.

Bug generation Process

    Jsonp (URL1, ajaxhandle, "xxx");     xxxx ");

When I write a call to the JSONP function, it looks like there's nothing wrong,
Two different global JSONP will be generated based on the timestamp

Jsonpcallback_date1 (),
Jsonpcallback_date2 ()

Timestamp function.
But it could actually return the same timestamp ... In other words, the second URL2 request function overrides the first request function. Causes the first URL1 not to parse correctly.
That is to say, JS almost at the same time call the two Jsonp function ...

Solution, add a random number, if the random number can also be a staggering coincidence, can only hehe.

var function (URL, func, type) {    varnew Date (). GetTime () + Math.floor (math.random () * 1000000), c7/>= URL + (url.indexof ('? ') + 1?) ' & ': '? ') + ' callback= ' + callback,        = createlement ("script", {"src": url});     // Console.log (Callback, "callback")    function (data) {        func (data, type);    };    Document.body.appendChild (s);}

The outrageous JS run speed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.