温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

getScript缓存响应实例分析

发布时间:2022-03-28 14:36:59 来源:亿速云 阅读:158 作者:iii 栏目:web开发

本篇内容介绍了“getScript缓存响应实例分析”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

  Caching Responses(缓存响应)

  默认情况下,$.getScript() cache选项被设置为 false。这将在请求的URL的查询字符串中追加一个时间戳参数,以确保每次浏览器下载的脚本被重新请求。您可以全局的使用 $.ajaxSetup()设置cache(缓存)属性覆盖该功能:

  $.ajaxSetup({

  cache: true

  });

  另外,  你可以使用更灵活的 $.ajax() 方法定义一个新的方法

  例子:

  Example: 定义了一个 $.cachedScript() 方法可以获取缓存的脚本:

  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: 我们动态加载新的官方jQuery 颜色动画插件,一旦该插件加载完成就会触发一些颜色动画。

  <!DOCTYPE html>

  <html>

  <head>

  <style>

  .block {

  background-color: blue;

  width: 150px;

  height: 70px;

  margin: 10px;

  }</style>

  <script src="https://code.jquery.com/jquery-latest.js"></script>

  </head>

  <body>

  <button id="go">&raquo; Run</button>

  <div class="block"></div>

  <script>

  (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 );

  });

  });

  })();

  </script>

  </body>

  </html>

“getScript缓存响应实例分析”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI