Get the App
SLTechnology News&Howtos  ›  Development  › 

Case Analysis of getScript Cache response

Shulou Source: shulou.com Published: 2022-06-01 03:06:18 09月22日 Update

This article introduces the relevant knowledge of "getScript cache response case analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Caching Responses (cached response)

By default, the $.getScript () cache option is set to false. This appends a timestamp parameter to the query string of the requested URL to ensure that each script downloaded by the browser is re-requested. You can override this feature globally with the $.ajaxSetup () setting cache (cache) property:

$.ajaxSetup ({

Cache: true

})

In addition, you can define a new method using the more flexible $.ajax () method

Example:

Example: defines a script where the $.cachedScript () method can get the cache:

JQuery.cachedScript = function (url, options) {

/ / allow user to set any option except for dataType, cache, and url

Options = $.extend (options | | {}, {

DataType: "script"

Cache: true

Url: url

})

/ / Use $.ajax () since it is more flexible than $.getScript

/ / Return the jqXHR object so we can chain callbacks

Return jQuery.ajax (options)

}

/ / Usage

$.cachedScript ("ajax/test.js") .done (function (script, textStatus) {

Console.log (textStatus)

})

Example: we dynamically load the new official jQuery color animation plug-in, which will trigger some color animation once the plug-in is loaded.

.block {

Background-color: blue

Width: 150px

Height: 70px

Margin: 10px

}

»Run

(function () {

Var url = "https://raw.github.com/jquery/jquery-color/master/jquery.color.js";

$.getScript (url, function () {

$("# go") .click (function () {

$(".block")

.animate ({backgroundColor: "rgb (255,180,180)"}, 1000)

.delay (500)

.animate ({backgroundColor: "olive"}, 1000)

.delay (500)

.animate ({backgroundColor: "# 00f"}, 1000)

})

})

) ()

This is the end of the content of "getScript caching response instance Analysis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Cache method instance instance analysis analysis content animation situation plug-in more knowledge script color practical learned next example global functional dynamic Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Information Linux Docker OPPO Reno