From 33ec16aa0946deff59494828077a5ccb8428609c Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 11 Nov 2018 03:40:19 +0800 Subject: [PATCH] feat/fix(more-comment): avoid duplicate listener --- src/disqus.js | 14 ++++++++++---- src/disqusjs.css | 1 - 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/disqus.js b/src/disqus.js index 2c534f5..f1629df 100644 --- a/src/disqus.js +++ b/src/disqus.js @@ -16,6 +16,7 @@ * * DisqusJS Info * @param {string} disqusjs.page.id = The thread id, used at next API call + * @param {string} disqusjs.page.next = The cursor of next page of list * @param {boolean} disqusjs.page.isClosed - Whether the comment is closed * @param {number} disqusjs.page.length - How many comment in the thread */ @@ -270,6 +271,12 @@ function DisqusJS(config) { * @param {string} cursor - 传入 cursor 用于加载下一页的评论 */ let getComment = (cursor) => { + let getMoreComment = () => { + // 执行完以后去除当前监听器避免重复调用 + $loadMoreBtn.removeEventListener('click', getMoreComment); + // 加载下一页评论 + getComment(disqusjs.page.next); + } var $loadMoreBtn = document.getElementById('dsqjs-load-more'); // 处理传入的 cursor if (!cursor) { @@ -313,14 +320,13 @@ function DisqusJS(config) { if (res.cursor.hasNext) { + // 将 cursor.next 存入 disqusjs 变量中供不能传参的不匿名函数使用 + disqusjs.page.next = res.cursor.next; // 确保 加载更多评论按钮 文字正常 $loadMoreBtn.innerHTML = '加载更多评论' // 显示 加载更多评论 按钮 $loadMoreBtn.classList.remove('dsqjs-hide'); - $loadMoreBtn.addEventListener('click', () => { - // 递归调用 getComment,传入 cursor 参数 - getComment(res.cursor.next) - }); + $loadMoreBtn.addEventListener('click', getMoreComment); } else { // 没有更多评论了,确保按钮隐藏 $loadMoreBtn.classList.add('dsqjs-hide'); diff --git a/src/disqusjs.css b/src/disqusjs.css index 3fe508c..c569d60 100644 --- a/src/disqusjs.css +++ b/src/disqusjs.css @@ -16,7 +16,6 @@ } #dsqjs .dsqjs-disabled { - pointer-events: none; cursor: not-allowed; opacity: .5; }