{"id":160141,"date":"2024-05-08T13:24:10","date_gmt":"2024-05-08T05:24:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/160141.html"},"modified":"2024-05-08T13:24:15","modified_gmt":"2024-05-08T05:24:15","slug":"%e5%89%8d%e7%ab%af-javascript-%e7%bc%96%e7%a8%8b%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e7%bc%93%e5%ad%98%e5%92%8c%e6%b8%85%e9%99%a4%e7%bc%93%e5%ad%98-api","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/160141.html","title":{"rendered":"\u524d\u7aef Javascript \u7f16\u7a0b\u5982\u4f55\u83b7\u53d6\u7f13\u5b58\u548c\u6e05\u9664\u7f13\u5b58 API"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26222714\/8fecacec-097c-463b-a228-9333e90e4c4e.webp\" alt=\"\u524d\u7aef Javascript \u7f16\u7a0b\u5982\u4f55\u83b7\u53d6\u7f13\u5b58\u548c\u6e05\u9664\u7f13\u5b58 API\" \/><\/p>\n<p><p>\u5728\u524d\u7aefJavaScript\u7f16\u7a0b\u4e2d\uff0c<strong>\u83b7\u53d6\u548c\u6e05\u9664\u7f13\u5b58<\/strong>\u662f\u4f18\u5316\u5e94\u7528\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\u7684\u91cd\u8981\u624b\u6bb5\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5176\u4e2d\u5305\u62ec<strong>\u4f7f\u7528\u6d4f\u89c8\u5668\u7684LocalStorage\u3001SessionStorage\u3001\u4ee5\u53ca\u4f7f\u7528\u7f13\u5b58API<\/strong>\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5141\u8bb8\u5f00\u53d1\u8005\u6709\u6548\u7ba1\u7406\u5ba2\u6237\u7aef\u5b58\u50a8\u7684\u6570\u636e\uff0c\u63d0\u5347\u9875\u9762\u52a0\u8f7d\u901f\u5ea6\u3001\u51cf\u5c11\u670d\u52a1\u5668\u8d1f\u62c5\u3002\u7279\u522b\u662f\u7f13\u5b58API\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u6765\u63a7\u5236\u8d44\u6e90\u7684\u7f13\u5b58\u903b\u8f91\uff0c\u975e\u5e38\u9002\u5408\u8fdb\u884c\u590d\u6742\u7684\u7f13\u5b58\u64cd\u4f5c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528JavaScript\u64cd\u4f5c\u8fd9\u4e9b\u7f13\u5b58\u6280\u672f\uff0c\u7279\u522b\u662f\u7f13\u5b58API\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528LocalStorage\u548cSessionStorage\u83b7\u53d6\u548c\u6e05\u9664\u7f13\u5b58<\/h3>\n<\/p>\n<p><p><strong>LocalStorage\u548cSessionStorage<\/strong>\u662fWeb\u5b58\u50a8API\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u540c\u6b65\u952e\u503c\u5bf9\u5b58\u50a8\u673a\u5236\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u83b7\u53d6LocalStorage\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u4eceLocalStorage\u83b7\u53d6\u6570\u636e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>localStorage.getItem(key)<\/code>\u65b9\u6cd5\uff0c\u5176\u4e2d<code>key<\/code>\u662f\u4f60\u60f3\u8981\u83b7\u53d6\u6570\u636e\u7684\u952e\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u6b64\u65b9\u6cd5\u8fd4\u56de<code>null<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6e05\u9664LocalStorage\u7f13\u5b58<\/strong><\/p>\n<\/p>\n<p><p>\u6e05\u9664LocalStorage\u4e2d\u7684\u6570\u636e\u6709\u51e0\u79cd\u65b9\u5f0f\u3002\u4f7f\u7528<code>localStorage.removeItem(key)<\/code>\u53ef\u4ee5\u5220\u9664\u6307\u5b9a\u7684\u952e\u53ca\u5176\u5bf9\u5e94\u7684\u503c\u3002\u5982\u679c\u4f60\u60f3\u6e05\u9664LocalStorage\u4e2d\u7684\u6240\u6709\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>localStorage.clear()<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>SessionStorage\u7684\u64cd\u4f5c\u4e0eLocalStorage\u76f8\u4f3c<\/strong>\uff0c\u4f46\u5176\u5b58\u50a8\u7684\u6570\u636e\u5728\u9875\u9762\u4f1a\u8bdd\u7ed3\u675f\u65f6\u4f1a\u88ab\u6e05\u9664\uff0c\u4f7f\u5176\u7406\u60f3\u7528\u4e8e\u5b58\u50a8\u654f\u611f\u4fe1\u606f\u6216\u4e34\u65f6\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u7f13\u5b58API\u7ba1\u7406\u7f13\u5b58<\/h3>\n<\/p>\n<p><p><strong>\u7f13\u5b58API<\/strong>\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u63a7\u5236\u9009\u9879\uff0c\u5141\u8bb8\u7cbe\u7ec6\u7ba1\u7406\u6d4f\u89c8\u5668\u7f13\u5b58\u4e2d\u7684\u8d44\u6e90\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u83b7\u53d6\u7f13\u5b58\u4e2d\u7684\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7f13\u5b58API\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>caches.open(cacheName)<\/code>\u6253\u5f00\u4e00\u4e2a\u7f13\u5b58\uff0c\u7136\u540e\u4f7f\u7528<code>cache.match(request)<\/code>\u6765\u68c0\u7d22\u7279\u5b9a\u8bf7\u6c42\u7684\u54cd\u5e94\u3002\u8fd9\u91cc\u7684<code>request<\/code>\u53ef\u4ee5\u662f\u4e00\u4e2aURL\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e00\u4e2a<code>Request<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6e05\u9664\u7f13\u5b58API\u4e2d\u7684\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u6e05\u9664\u7f13\u5b58\u4e2d\u7684\u6570\u636e\u9700\u8981\u4f7f\u7528<code>caches.delete(cacheName)<\/code>\u65b9\u6cd5\uff0c\u8fd9\u5c06\u5b8c\u5168\u5220\u9664\u6307\u5b9a\u7684\u7f13\u5b58\u3002\u5982\u679c\u4f60\u9700\u8981\u4ece\u7f13\u5b58\u4e2d\u79fb\u9664\u7279\u5b9a\u7684\u9879\u76ee\uff0c\u4f60\u53ef\u4ee5\u5148\u6253\u5f00\u7f13\u5b58\uff0c\u7136\u540e\u4f7f\u7528<code>cache.delete(request)<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u5b9e\u8df5\u6848\u4f8b\u4e0e\u6280\u5de7<\/h3>\n<\/p>\n<p><p>\u5c06\u7406\u8bba\u8fd0\u7528\u5230\u5b9e\u8df5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u53d1\u51fa\u5177\u6709\u9ad8\u6548\u7f13\u5b58\u7ba1\u7406\u80fd\u529b\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u7f13\u5b58\u7b56\u7565\u5b9e\u73b0\u6848\u4f8b<\/strong><\/p>\n<\/p>\n<p><p>\u63a2\u8ba8\u4e00\u4e9b\u7f16\u7a0b\u6848\u4f8b\uff0c\u6bd4\u5982\u5982\u4f55\u7ed3\u5408LocalStorage\u548c\u7f13\u5b58API\u5b9e\u73b0\u79bb\u7ebf\u5e94\u7528\u529f\u80fd\uff0c\u6216\u8005\u5982\u4f55\u4f7f\u7528SessionStorage\u8fdb\u884c\u4f1a\u8bdd\u671f\u95f4\u6570\u636e\u7684\u5feb\u901f\u8bbf\u95ee\u548c\u7ba1\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u4f18\u5316\u548c\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<\/p>\n<p><p>\u8ba8\u8bba\u4f7f\u7528\u8fd9\u4e9b\u7f13\u5b58\u6280\u672f\u65f6\u7684\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u4ee5\u786e\u4fdd\u63d0\u4f9b\u5feb\u901f\u54cd\u5e94\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u540c\u65f6\u4e0d\u727a\u7272\u7528\u6237\u7684\u6570\u636e\u5b89\u5168\u548c\u9690\u79c1\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u7ed3\u8bed<\/h3>\n<\/p>\n<p><p><strong>\u7f13\u5b58\u7ba1\u7406<\/strong>\u662f\u524d\u7aef\u5f00\u53d1\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\uff0c\u5408\u7406\u4f7f\u7528JavaScript\u63d0\u4f9b\u7684\u5b58\u50a8\u548c\u7f13\u5b58API\u80fd\u591f\u663e\u8457\u63d0\u9ad8\u5e94\u7528\u7684\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\u3002\u4e86\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u6280\u672f\uff0c\u80fd\u5e2e\u52a9\u5f00\u53d1\u8005\u6784\u5efa\u66f4\u5feb\u3001\u66f4\u9ad8\u6548\u3001\u66f4\u53ef\u9760\u7684Web\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: \u5982\u4f55\u4f7f\u7528 JavaScript \u83b7\u53d6\u7f13\u5b58\u7684\u5185\u5bb9\uff1f<\/strong><\/p>\n<p>A: \u4f60\u53ef\u4ee5\u4f7f\u7528 JavaScript \u7684\u7f13\u5b58 API \u6765\u8bbf\u95ee\u6d4f\u89c8\u5668\u7f13\u5b58\u4e2d\u7684\u6570\u636e\u3002\u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>caches<\/code>\u5bf9\u8c61\u548c<code>cacheStorage<\/code>\u63a5\u53e3\u3002 \u4f60\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e9b API \u6765\u83b7\u53d6\u7f13\u5b58\u7684\u952e\u503c\u5bf9\uff0c\u68c0\u67e5\u7f13\u5b58\u662f\u5426\u5b58\u5728\u3001\u83b7\u53d6\u7f13\u5b58\u7684\u8be6\u7ec6\u4fe1\u606f\u7b49\u3002    <\/p>\n<p><strong>Q: \u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u6e05\u9664 JavaScript \u7f13\u5b58\uff1f<\/strong><\/p>\n<p>A: \u6e05\u9664 JavaScript \u7f13\u5b58\u6709\u591a\u79cd\u65b9\u6cd5\u3002\u9996\u5148\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>caches.delete()<\/code>\u65b9\u6cd5\u6765\u5220\u9664\u6307\u5b9a\u7684\u7f13\u5b58\u3002\u8fd9\u4e2a\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u7f13\u5b58\u540d\u79f0\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5c06\u8be5\u7f13\u5b58\u4ece\u6d4f\u89c8\u5668\u4e2d\u5220\u9664\u3002\u53e6\u5916\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>caches.keys()<\/code>\u6765\u83b7\u53d6\u7f13\u5b58\u7684\u5217\u8868\uff0c\u7136\u540e\u5faa\u73af\u904d\u5386\u5e76\u4f7f\u7528<code>caches.delete()<\/code>\u6765\u9010\u4e2a\u5220\u9664\u3002\u8fd8\u6709\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528<code>caches.delete()<\/code>\u6765\u6e05\u9664\u6240\u6709\u7684\u7f13\u5b58\uff0c\u8fd9\u6837\u53ef\u4ee5\u4e00\u6b21\u6027\u6e05\u7406\u6389\u6240\u6709\u7684\u7f13\u5b58\u6570\u636e\u3002<\/p>\n<p><strong>Q: \u9664\u4e86\u4f7f\u7528 JavaScript \u7684\u7f13\u5b58 API \u8fdb\u884c\u7f13\u5b58\u7ba1\u7406\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u7684\u65b9\u6cd5\u5417\uff1f<\/strong><\/p>\n<p>A: \u662f\u7684\uff0c\u9664\u4e86\u4f7f\u7528 JavaScript \u7684\u7f13\u5b58 API\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u65b9\u6cd5\u7528\u4e8e\u7f13\u5b58\u7ba1\u7406\u3002\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u662f\u5728\u7f51\u9875\u7684 <code>&lt;head&gt;<\/code> \u6807\u7b7e\u4e2d\u4f7f\u7528<code>&lt;meta&gt;<\/code>\u6807\u7b7e\u6765\u8bbe\u7f6e\u7f13\u5b58\u7b56\u7565\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>&lt;meta http-equiv=&quot;Cache-Control&quot; content=&quot;no-cache&quot;&gt;<\/code>\u6765\u7981\u7528\u7f13\u5b58\uff0c\u6216\u8005\u4f7f\u7528<code>&lt;meta http-equiv=&quot;Cache-Control&quot; content=&quot;max-age=3600&quot;&gt;<\/code> \u8bbe\u7f6e\u7f13\u5b58\u7684\u6700\u5927\u5b58\u6d3b\u65f6\u95f4\u3002\u53e6\u5916\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5728\u670d\u52a1\u5668\u7aef\u8bbe\u7f6e HTTP \u54cd\u5e94\u5934\u6765\u63a7\u5236\u7f13\u5b58\u7b56\u7565\u3002\u901a\u8fc7\u5728\u54cd\u5e94\u5934\u4e2d\u8bbe\u7f6e<code>Cache-Control<\/code>\u548c<code>Expires<\/code>\u5b57\u6bb5\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u7f13\u5b58\u7684\u63a7\u5236\u7b56\u7565\uff0c\u6bd4\u5982\u8bbe\u7f6e\u7f13\u5b58\u7684\u6700\u5927\u5b58\u6d3b\u65f6\u95f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u524d\u7aefJavaScript\u7f16\u7a0b\u4e2d\uff0c\u83b7\u53d6\u548c\u6e05\u9664\u7f13\u5b58\u662f\u4f18\u5316\u5e94\u7528\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\u7684\u91cd\u8981\u624b\u6bb5\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5176 [&hellip;]","protected":false},"author":3,"featured_media":160144,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/160141"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=160141"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/160141\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/160144"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=160141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=160141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=160141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}