1

我的评论系统的评论代码遇到了这个奇怪的问题。我添加了一个功能,您必须在每条评论后等待 10 秒。它以前工作得很好,但现在我稍微更改了代码,我遇到了问题。当您发表评论时,即使这是您发布的第一条评论,也会出现错误,这应该是因为计时器甚至不应该处于活动状态。

完整的代码是:

1    var h = 0,
2       i, j = 10;
3    $(".comment").click(function () {
4       if (h == 0) {
5           var a = $(this).attr("data-post"),
6               b = $(".comname-" + a).text(),
7               c = $(".combody-" + a).val();
8           if ($.trim(b) == "") {
9               alertBox("Missing Field", "Please Enter a Name to Comment.", "Close")
10          }
11          else if ($.trim(c) == "") {
12              alertBox("Missing Field", "Please Enter a Comment.", "Close")
13          }
14          else {
15              var d = true;
16              if (c.length > 54) {
17                  if ($$.string(c).count(" ") < 3) {
18                      d = false
19                  }
20              }
21              if (d == true) {
22                  $.post("post.php?action=comment", {
23                      id: a,
24                      name: b,
25                      com: c
26                  }, function (b) {
27                      var c = $$.string(b).found(/\[[0-9]\] => (.+)/g, 2);
28                      $(".no-" + a).hide();
29                      $(".comname-" + a + ", .combody-" + a).val("");
30                      $(".comholder-" + a).append('<div class="commenthold comment-' + c[3] + '"><span class="content-' + c[3] + '">' + c[1] + '</span> <br> <span class="name cname-' + c[3] + '">' + c[0] + '</span><span class="comment-timeago" style="float:right" title="' + c[2] + '"></span><img src="/img/edit.png" class="edit tip" title="Edit Comment" style="cursor:pointer" data-id="' + a + '" onclick="openEdit(' + c[3] + ')"/><img src="/img/edited.png" title="This comment has be edited 0 times" class="times-' + c[3] + '" style="display:none;cursor:pointer;width: 13px;  height: 13px;  position: absolute;  top: 0px;  right: -17px;"><div style="color:red;position:absolute;top:2px;right:4px;cursor:pointer" title="Delete Comment" data-id="' + a + '" onclick="delO(' + a + ',' + c[3] + ')" class="tip"><img class="width" src="/img/remove.png" /></div></div><br>');
31                      $(".tip").tipsy({
32                          gravity: "sw"
33                      });
34                      $(".comment-timeago").timeago();
35                      var d = parseInt($(".show-" + a).text().replace(/[^0-9]/g, "")) + 1;
36                      $(".show-" + a).text(d + " Comment" + (d !== 1 ? "s" : ""))
37                  });
38                  h = 1;
39                  i = setTimeout(function () {
40                      h = 0
41                  }, j * 1e3);
42              }
43              else {
44                  alertBox("Error", "You must have at least 4 words considering the length of this comment.", "Close")
45              }
46          }
47      }
48      else {
49          clearTimeout(i);
50          i = setTimeout(function () {
51              h = 0
52          }, j * 1e3);
53          alertBox("Timer Warning", "Must wait " + j + " seconds before commenting again. Timer Reset.", "Close")
54      }
55    }); 

奇怪的是,如果我在开始之前添加一个警报,if (h == 0)那么它可以正常工作并且它不会显示计时器的错误框。

但是,如果我将 console.log 记录h为等于它将记录两次,这意味着它正在运行代码两次。即使它似乎只运行一次警报。

控制台(萤火虫):

控制台

看?

代码解释。

我知道仅仅看代码会使理解代码有点困难。我想让你更轻松。

第 1-2 行:定时器的变量。h是状态,0 = 你可以评论;1 = 定时器仍未结束。i是 setTimeout 将被存储的变量。j是您可以再次评论之前的秒数。
第 4 行:开始if确保计时器结束。
第 5-7 行:获取帖子 ID(data-post)、评论者姓名(comnam)和获取实际评论(combody)的更多变量
第 8-13 行:确保名称并且评论中有内容。
第 15 行:对于另一种类型的错误。
第 16-20 行:如果评论超过 55 个字符,则检查它是否超过 3 个单词。如果不d== false 这将触发错误。
第 27 行:这个变量包含一个特殊的函数,它使用一个小的 RegExp 来获取所有信息b(来自 PHP 的信息,例如时间戳)。
第 28 行:隐藏“还没有评论!成为第一个!” 如果还没有评论。
第 29 行:清除名称和注释字段。
第 30 行:将评论附加到评论区域。
第 35-36 行:更新告诉您有多少评论的文本。
第 39-41 行:启动计时器,10000 毫秒(10 秒)后变h回 0,以便您可以继续评论。
第 50-52 行:重置计时器

我真的不明白问题是什么以及为什么它运行代码两次。我的代码中没有任何内容可以再次运行它。

感谢您的帮助,我希望我的问题对您来说已经足够清楚了。

4

0 回答 0