JavaScript   发布时间:2022-04-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了es6学习笔记之Async函数基本教程大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

本文介绍的是关于es6中Async函数的相关内容,非常出来供大家参学习,需要的朋友们下面来看看详细的介绍:

async 函数

async 函数,使得异步操作变得更加方便。它是 Generator 函数的语法糖。

Generator 函数,依次读取两个文件:

require('fs'); var readFile = function (fileName) { return new Promise(function (resolve,reject) { fs.readFile(fil@R_616_8371@,function(error,data) { if (error) reject(error); resolve(data); }); }); }; var gen = function* () { var f1 = yield readFile('/etc/fstab'); var f2 = yield readFile('/etc/sHells'); console.log(f1.toString()); console.log(f2.toString()); };

写成async函数,就是下面这样:

{ var f1 = await readFile('/etc/fstab'); var f2 = await readFile('/etc/sHells'); console.log(f1.toString()); console.log(f2.toString()); };

async函数对 Generator 函数的改进,体现在以下四点:

1)内置执行器

Generator 函数的执行必须靠执行器,所以才有了co模块,而Async函数自带执行器。也就是说,async函数的执行,与普通函数一模一样,只要一行。

);

上面的代码调用了asyncReadFile函数,然后它就会自动执行,输出最后结果。这完全不像 Generator 函数,需要调用next方法,或者用co模块,才能真正执行,得到最后结果。

2)更好的语义

async和await,比起星号和yield,语义更清楚了。async表示函数里有异步操作,await表示紧跟在后面的表达式需要等待结果。

3)更广的适用性

co模块约定,yield命令后面只能是 Thunk 函数或 Promise 对象,而Async函数的await命令后面,可以是Promise 对象和原始类型的值(数值、字符串和布尔值,但这时等同于同步操作)。

4)返回值是 Promise

async函数的返回值是 Promise 对象,这比 Generator 函数的返回值是 Iterator 对象方便多了。你可以用then方法指定下一步的操作。

进一步说,async函数完全可以看作多个异步操作,包装成的一个 Promise 对象,而Await命令就是内部then命令的语法糖。

一、基本用法

async函数返回一个 Promise 对象,可以使用then方法添加回调函数。当函数执行的时候,一旦遇到await就会先返回,等到异步操作完成,再接着执行函数体内后面的语句。

{ setTimeout(resolve,ms); }); } async function asyncPrint(value,ms) { await timeout(ms); console.log(value); } asyncPrint('Hello world',5000);

上面代码指定5000毫秒以后,输出Hello world。

由于async函数返回的是 Promise 对象,可以作为await命令的参数。所以,上面的例子也可以写成下面的形式:

{ setTimeout(resolve,5000);

async 函数多种使用形式

{};

二、语法

async函数的语法规则总体上比较简单,难点是错误处理机制。

返回 Promise 对象

async函数返回一个 Promise 对象。async函数内部return语句返回的值,会成为then方法回调函数的参数。

console.log(v)) // "Hello world"

Promise 对象的状态变化

async函数返回的 Promise 对象,必须等到内部所有await命令后面的 Promise 对象执行完,才会发生状态改变,除非遇到return语句或者抛出错误。也就是说,只有async函数内部的异步操作执行完,才会执行then方法指定的回调函数。

title(url) { let response = await fetch(url); let html = await response.text(); return html.match(/([\s\S]+)<\/title>/i)[1]; } get<a href="http://code.js-code.com/tag/titl/" target="_blank" class="keywords">titl</a>e('<a href="http://code.js-code.com/tag/http/" target="_blank" class="keywords">http</a>s://tc39.github.io/ecma<a href="http://code.js-code.com/tag/262/" target="_blank" class="keywords">262</a>/').then(console.log) // "ECMAScript 2017 Language Specification"</pre> </div> <p>上面代码中,函数get<a href="http://code.js-code.com/tag/titl/" target="_blank" class="keywords">titl</a>e内部有三个操作:抓取网页、取出文本、匹配页面标题。只有这三个操作全部完成,才会执行then方法里面的console.log。</p> <p><h3>三、使用注意点</h3></p> <p>await命令<a href="http://code.js-code.com/tag/houmiande/" target="_blank" class="keywords">后面的</a>Promise对象,运行结果可能是re<a href="http://code.js-code.com/tag/je/" target="_blank" class="keywords">je</a>cted,所以最好把await命令放在try…catch代码块中。</p> <div class="jb51code"> <pre class="brush:js;"> async function <a href="http://code.js-code.com/tag/MyFunc/" target="_blank" class="keywords">MyFunc</a>tion() <a href="http://code.js-code.com/tag/" target="_blank" class="keywords">{</a> try <a href="http://code.js-code.com/tag/" target="_blank" class="keywords">{</a> await somethingThatReturnsAPromise(<a href="http://code.js-code.com/tag/" target="_blank" class="keywords">);</a> } catch (err) <a href="http://code.js-code.com/tag/" target="_blank" class="keywords">{</a> console.log(err<a href="http://code.js-code.com/tag/" target="_blank" class="keywords">);</a> } } // 另一种写法 async function <a href="http://code.js-code.com/tag/MyFunc/" target="_blank" class="keywords">MyFunc</a>tion() <a href="http://code.js-code.com/tag/" target="_blank" class="keywords">{</a> await somethingThatReturnsAPromise() .catch(function (err) <a href="http://code.js-code.com/tag/" target="_blank" class="keywords">{</a> console.log(err<a href="http://code.js-code.com/tag/" target="_blank" class="keywords">);</a> }; }</pre> </div> <p>多个await命令<a href="http://code.js-code.com/tag/houmiande/" target="_blank" class="keywords">后面的</a>异步操作,如果不存在继发关系,最好让它们同时触发。</p> <div class="jb51code"> <pre class="brush:js;"> //异步操作(即互不依赖),被写成继发关系。这样比较耗时,因为只有getFoo完成以后,才会执行getBar,<a href="http://code.js-code.com/tag/wanquanke/" target="_blank" class="keywords">完全可</a>以让它们同时触发。 let foo = await getFoo(<a href="http://code.js-code.com/tag/" target="_blank" class="keywords">);</a> let bar = await getBar(<a href="http://code.js-code.com/tag/" target="_blank" class="keywords">);</a> // 写法一 let [foo,bar] = await Promise.all([getFoo(),getBar()]<a href="http://code.js-code.com/tag/" target="_blank" class="keywords">);</a> // 写法二 let fooPromise = getFoo(<a href="http://code.js-code.com/tag/" target="_blank" class="keywords">);</a> let barPromise = getBar(<a href="http://code.js-code.com/tag/" target="_blank" class="keywords">);</a> let foo = await fooPromise; let bar = await barPromise;</pre> </div> <p>await命令只能用在async函数之中,如果用在普通函数,就会报错。</p> <p>更多关于async函数的使用例子可以点击查看这篇文章:<a href="//cn.js-code.com/article/113475.htm">//cn.js-code.com/article/113475.htm</a></p> <p><h3>总结</h3></p> <p>以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对菜鸟教程的支持。</p> <h2>大佬总结</h2> <p>以上是<a href="http://code.js-code.com" title="http://code.js-code.com">大佬教程</a>为你收集整理的<a href="http://code.js-code.com/javascript/276206.html" class="article_link" target="_blank">es6学习笔记之Async函数基本教程</a>全部内容,希望文章能够帮你解决<a href="http://code.js-code.com/javascript/276206.html" class="article_link" target="_blank">es6学习笔记之Async函数基本教程</a>所遇到的程序开发问题。</p> <p>如果觉得<a href="http://code.js-code.com" title="http://code.js-code.com">大佬教程</a>网站内容还不错,欢迎将<a href="http://code.js-code.com" title="http://code.js-code.com">大佬教程</a>推荐给程序员好友。</p> <blockquote>本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。<br>如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。</blockquote> </div> <div class="tags text-center clearfix center-block">标签:<a href="http://code.js-code.com/tag/async/" class="tag_link" target="_blank">async</a><a href="http://code.js-code.com/tag/asynchanshu/" class="tag_link" target="_blank">async函数</a><a href="http://code.js-code.com/tag/es6/" class="tag_link" target="_blank">es6</a></div> </div> <div class="panel panel-default"> <ul class="list-group"> <li class="list-group-item"><a href="http://code.js-code.com/javascript/276205.html" class="prev" target="_self"> 上一篇: Angularjs 与 bower安装和使用详...</a><a href="http://code.js-code.com/javascript/276207.html" class="text-muted pull-right" target="_self"> 下一篇:canvas实现弧形可拖动进度条效果</a></li> </ul> </div> <div class="panel panel-default"> </div> <div class="panel panel-default"> <div class="panel-heading font-weight-bold">猜你在找的JavaScript相关文章</div> <ul class="list-group"><li class="list-group-item"> <a href="http://code.js-code.com/javascript/284450.html" title="JavaScript实现页面5秒后自动跳转的方法">JavaScript实现页面5秒后自动跳转的方法</a> <span class="text-muted pull-right">2022-04-16</span> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/271645.html" title="JS获取前天、昨天、今天、明天、后天的时间">JS获取前天、昨天、今天、明天、后天的时间</a> <span class="text-muted pull-right">2022-04-16</span> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/282665.html" title="推荐阅读的js快速判断IE浏览器(兼容IE10与IE11)">推荐阅读的js快速判断IE浏览器(兼容IE10与IE11)</a> <span class="text-muted pull-right">2022-04-16</span> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/1204.html" title="JavaScript单位换算和数字格式化">JavaScript单位换算和数字格式化</a> <span class="text-muted pull-right">2019-10-09</span> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/1203.html" title="JavaScript 运行时错误: 无法获取未定义或 null 引用的属性,怎么办?">JavaScript 运行时错误: 无法获取未定义或 null 引用的属性,怎么办?</a> <span class="text-muted pull-right">2019-10-09</span> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/286611.html" title="如何使用Javascript获取元素的不透明度?">如何使用Javascript获取元素的不透明度?</a> <span class="text-muted pull-right">2022-04-16</span> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/1201.html" title="JAVASCRIPT中URL 传递参数(特殊字符)解决方法及转码解码的介绍">JAVASCRIPT中URL 传递参数(特殊字符)解决方法及转码解码的介绍</a> <span class="text-muted pull-right">2019-10-09</span> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/1200.html" title="JavaScript中_proto_和prototype的区别">JavaScript中_proto_和prototype的区别</a> <span class="text-muted pull-right">2019-10-09</span> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/1199.html" title="【Javascript】【DOM操作】获取checkbox的checked属性">【Javascript】【DOM操作】获取checkbox的checked属性</a> <span class="text-muted pull-right">2019-10-09</span> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/1197.html" title="如何理解JavaScript权威指南第96页的“也就是说,可以在声明一个JavaScipt函数之前调用它”">如何理解JavaScript权威指南第96页的“也就是说,可以在声明一个JavaScipt函数之前调用...</a> <span class="text-muted pull-right">2019-10-09</span> </li></ul> </div> <div class="panel panel-default"> <div class="panel-heading"><a href="https://www.js-code.com/all" class="font-weight-bold">其他相关热搜词</a><span class="pull-right"><a href="https://www.js-code.com/all">更多</a></span></div> <div class="panel-body list-cloud"><a href="http://code.js-code.com/tag/php/">php</a><a href="http://code.js-code.com/tag/Java/">Java</a><a href="http://code.js-code.com/tag/Python/">Python</a><a href="http://code.js-code.com/tag/chengxuyuan/">程序员</a><a href="http://code.js-code.com/tag/load/">load</a><a href="http://code.js-code.com/tag/zhong/">中</a><a href="http://code.js-code.com/tag/ruhe/">如何</a><a href="http://code.js-code.com/tag/string/">string</a><a href="http://code.js-code.com/tag/shiyong/">使用</a><a href="http://code.js-code.com/tag/canshu/">参数</a><a href="http://code.js-code.com/tag/jquery/">jquery</a><a href="http://code.js-code.com/tag/kaifa/">开发</a><a href="http://code.js-code.com/tag/anzhuang/">安装</a><a href="http://code.js-code.com/tag/list/">list</a><a href="http://code.js-code.com/tag/linux/">linux</a><a href="http://code.js-code.com/tag/ios/">ios</a><a href="http://code.js-code.com/tag/android/">android</a><a href="http://code.js-code.com/tag/gongju/">工具</a><a href="http://code.js-code.com/tag/javascript/">javascript</a><a href="http://code.js-code.com/tag/cap/">cap</a></div> </div> </div> <div class="col-sm-3"> <div style="position:fixed!important;top:334px;height:550px;z-index:9999;"> <div class="_wfhdnfcjs9"></div> <script type="text/javascript"> (window.slotbydup = window.slotbydup || []).push({ id: "u6828100", container: "_wfhdnfcjs9", async: true }); </script> <!-- 多条广告如下脚本只需引入一次 --> <script type="text/javascript" src="//cpro.baidustatic.com/cpro/ui/cm.js" async="async" defer="defer" > </script> </div> <div class="panel panel-default" style="display: none;"> <div class="panel-heading font-weight-bold">最新JavaScript教程</div> <ul class="list-group"><li class="list-group-item"> <a href="http://code.js-code.com/javascript/286611.html" title="如何使用Javascript获取元素的不透明度?">如何使用Javascript获取元素的不透明度?</a> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/286610.html" title="javascript – 如何扩展谷歌分析以跟踪AJAX等(根据H5BP文档)">javascript – 如何扩展谷歌分析以跟踪AJAX等(根据H5BP文档)</a> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/286609.html" title="javascript – Webpack加载字体">javascript – Webpack加载字体</a> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/286608.html" title="javascript – Angular2’this’未定义">javascript – Angular2’this’未定义</a> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/286607.html" title="javascript – JQuery UI自动完成回调">javascript – JQuery UI自动完成回调</a> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/286606.html" title="在javascript中使用和捕获RangeError">在javascript中使用和捕获RangeError</a> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/286605.html" title="在javascript中将军事时间转换为标准时间的最佳方法">在javascript中将军事时间转换为标准时间的最佳方法</a> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/286604.html" title="javascript – 从getElementsByTagName()获取属性的最佳方法?">javascript – 从getElementsByTagName()获取属性的最佳方法?</a> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/286603.html" title="javascript – 如何在HTA的默认Web浏览器中打开链接?">javascript – 如何在HTA的默认Web浏览器中打开链接?</a> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/286602.html" title="javascript – WebGL iOS渲染到浮点纹理">javascript – WebGL iOS渲染到浮点纹理</a> </li></ul> </div> <div class="panel panel-default" style="display: none;"> <div class="panel-heading font-weight-bold">热门JavaScript教程</div> <ul class="list-group"><li class="list-group-item"> <a href="http://code.js-code.com/javascript/1208.html" title="Javascript, jQuery 各种height整理">Javascript, jQuery 各种height整理</a> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/1198.html" title="JavaScript: 奇数在左,偶数在右">JavaScript: 奇数在左,偶数在右</a> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/1205.html" title="JavaScript中的位运算">JavaScript中的位运算</a> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/1203.html" title="JavaScript 运行时错误: 无法获取未定义或 null 引用的属性,怎么办?">JavaScript 运行时错误: 无法获取未定义或 null 引用的属性,怎么办?</a> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/1196.html" title="javascript note">javascript note</a> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/286611.html" title="如何使用Javascript获取元素的不透明度?">如何使用Javascript获取元素的不透明度?</a> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/286610.html" title="javascript – 如何扩展谷歌分析以跟踪AJAX等(根据H5BP文档)">javascript – 如何扩展谷歌分析以跟踪AJAX等(根据H5BP文档)</a> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/286609.html" title="javascript – Webpack加载字体">javascript – Webpack加载字体</a> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/286608.html" title="javascript – Angular2’this’未定义">javascript – Angular2’this’未定义</a> </li><li class="list-group-item"> <a href="http://code.js-code.com/javascript/286607.html" title="javascript – JQuery UI自动完成回调">javascript – JQuery UI自动完成回调</a> </li></ul> </div> </div> </div> </div> <footer id="footer"> <div class="container"> <div class="row hidden-xs"> <dl class="col-sm-4 site-link"> <dt>最近更新</dt><dd> <a href="http://code.js-code.com/webqianduan/919677.html" title="面向前端开发人员的 20 个强大的 VS Code 扩展">面向前端开发人员的 20 个强大的...</a> <span class="text-muted pull-right">2024-04-28 </span> </dd><dd> <a href="http://code.js-code.com/webqianduan/919676.html" title="面向前端开发人员的 20 个强大的 VS Code 扩展">面向前端开发人员的 20 个强大的...</a> <span class="text-muted pull-right">2024-04-28 </span> </dd><dd> <a href="http://code.js-code.com/chengxubiji/919675.html" title="Linux——(1)基本命令">Linux——(1)基本命令</a> <span class="text-muted pull-right">2022-07-21 </span> </dd><dd> <a href="http://code.js-code.com/chengxubiji/919674.html" title="【UNIAPP】上传视频,进度条的前台与后端">【UNIAPP】上传视频,进度条的前...</a> <span class="text-muted pull-right">2022-07-21 </span> </dd><dd> <a href="http://code.js-code.com/chengxubiji/919673.html" title="You can't specify target table 'xxx' for update in FROM clause的解决">You can't specify target t...</a> <span class="text-muted pull-right">2022-07-21 </span> </dd><dd> <a href="http://code.js-code.com/chengxubiji/919672.html" title="jmeter压测,将jmeter返回数据,保存到表格">jmeter压测,将jmeter返回数据,...</a> <span class="text-muted pull-right">2022-07-21 </span> </dd><dd> <a href="http://code.js-code.com/chengxubiji/919671.html" title="经典区间dp 【方块消除】">经典区间dp 【方块消除】</a> <span class="text-muted pull-right">2022-07-21 </span> </dd><dd> <a href="http://code.js-code.com/chengxubiji/919670.html" title="java-day02">java-day02</a> <span class="text-muted pull-right">2022-07-21 </span> </dd><dd> <a href="http://code.js-code.com/chengxubiji/919669.html" title="【异常】 'ascii' codec can't decode byte 0xe8 in position 2: ordinal not in range(128)">【异常】 'ascii' cod...</a> <span class="text-muted pull-right">2022-07-21 </span> </dd><dd> <a href="http://code.js-code.com/chengxubiji/919668.html" title="Django后台admin常用设置">Django后台admin常用设置</a> <span class="text-muted pull-right">2022-07-21 </span> </dd></dl> <dl class="col-sm-4 site-link"> <dt>热门文章</dt><dd> <a href="http://code.js-code.com/php/8105.html" title="php实现mysql数据库分表分段备份">php实现mysql数据库分表分段备份</a> <span class="text-muted pull-right">2019-11-12 </span> </dd><dd> <a href="http://code.js-code.com/php/9790.html" title="php基于base64解码图片与加密图片还原实例">php基于base64解码图片与加密图片...</a> <span class="text-muted pull-right">2019-11-13 </span> </dd><dd> <a href="http://code.js-code.com/php/6138.html" title="自定义min版smarty模板引擎MinSmarty.class.php文件及用法">自定义min版smarty模板引擎MinSm...</a> <span class="text-muted pull-right">2019-11-12 </span> </dd><dd> <a href="http://code.js-code.com/php/6073.html" title="php自定义中文字符串截取函数substr_for_gb2312及substr_for_utf8示例">php自定义中文字符串截取函数sub...</a> <span class="text-muted pull-right">2019-11-12 </span> </dd><dd> <a href="http://code.js-code.com/php/4608.html" title="PHP使用preg_split()分割特殊字符(元字符等)的方法分析">PHP使用preg_split()分割特殊字符...</a> <span class="text-muted pull-right">2019-11-11 </span> </dd><dd> <a href="http://code.js-code.com/php/4416.html" title="PHP利用正则表达式将相对路径转成绝对路径的方法示例">PHP利用正则表达式将相对路径转成...</a> <span class="text-muted pull-right">2019-11-11 </span> </dd><dd> <a href="http://code.js-code.com/php/4124.html" title="PHP删除二维数组中相同元素及数组重复值的方法示例">PHP删除二维数组中相同元素及数组...</a> <span class="text-muted pull-right">2019-11-11 </span> </dd><dd> <a href="http://code.js-code.com/php/4079.html" title="php对xml文件的增删改查操作实现方法分析">php对xml文件的增删改查操作实现...</a> <span class="text-muted pull-right">2019-11-11 </span> </dd><dd> <a href="http://code.js-code.com/php/4028.html" title="PHP判断密码强度的方法详解">PHP判断密码强度的方法详解</a> <span class="text-muted pull-right">2019-11-11 </span> </dd><dd> <a href="http://code.js-code.com/php/3899.html" title="PHP简单计算两个时间差的方法示例">PHP简单计算两个时间差的方法示例</a> <span class="text-muted pull-right">2019-11-11 </span> </dd></dl> <dl class="col-sm-2 site-link"> <dt>好站推荐</dt> <dd><a target="_blank" href="http://www.js-code.com">脚本宝典</a></dd> </dl> </div> <div class="copyright"> Copyright © 2019 大佬教程<br> <a href="http://www.miibeian.gov.cn/" rel="nofollow"></a> <span class="ml5">大佬教程 版权所有 <a href="https://www.miitbeian.gov.cn" target="_blank">豫ICP备15014364号-1</a></span> </div> </div> </footer> <script> (function(){ var src = "https://jspassport.ssl.qhimg.com/11.0.1.js?d182b3f28525f2db83acfaaf6e696dba"; document.write('<script src="' + src + '" id="sozz"><\/script>'); })(); </script> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?7e7b41e04f68f42525f709a1dfb50437"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <script type="text/javascript">document.write(unescape("%3Cspan id='cnzz_stat_icon_1281094757'%3E%3C/span%3E%3Cscript src='https://s4.cnzz.com/z_stat.php%3Fid%3D1281094757%26show%3Dpic' type='text/javascript'%3E%3C/script%3E"));</script> <script src="http://code.js-code.com/template/www/desktop/static/r_js/readmore.js" type="text/javascript"></script> <script> const btw = new BTWPlugin(); btw.init({ id: 'codeContent', blogId: '29457-1651677100115-362', name: 'DearMrGao', qrcode: 'http://code.js-code.com/res/2022/05-04/23/ea106a76a9b6f2bfc9374cb2e57301e4.jpg', keyword: '暗号', }); </script> <script type="text/javascript"> let ad = sessionStorage.getItem('codejdad') if(ad=='y'){ $("#jdad").hide(); }else{ $("#jdad").show(); } $("#jdad").click(function(){ { sessionStorage.setItem('codejdad','y') $("#jdad").hide(); window.open('https://u.jd.com/cM7PKuj'); } }) </script> </body> </html>