为什么这个函数被调用两次?

时间:2017-06-08 13:32:45

标签: javascript jquery

我有一个对话框机器人要求发送电子邮件,在此功能中,最后它会在聊天中附加一个按钮。它只对电子邮件功能重复这一点,但所有其他人只需要一次。可能导致这种情况的原因是什么?

我的设置(有条件的电子邮件)

  if (email.length > 0 && email_confirmed == true) {
    $("#message").hide();
    send_msg(
      "One second, let me go check and see where your practicum is in the queue.",
      function() {
        send_msg(
          "It looks like your practicum will be graded by {{ DATE }}", function(){
          send_msg("If this seems too long we may be in a bit of a delay as our team grades all these by hand. ", function(){
            send_msg("If it is longer than 10 business days please post in our community as you may have submitted with another email address", function() {
            email_confirmed = false;
            $("#message").hide();
            $("#controls").append(
            '<button class="options one" value="certifications" type="button">My Certifications</button>'
            );
            })
          })
          })
        });
      }

单击“我的认证”按钮时会出现问题。它称之为条件

我的Certs条件

//If user has certification issues
if (b_val == 'certifications'){
    send_msg('Awesome! Did you know we have over 75,000 certified users?', function(){
    send_msg('Please select which area of the certification process you are having trouble with', function(){
        $("#controls").append(
        '<button class="options one" value="practicum" type="button">Practicum status</button>' +
        '<button class="options one" value="exams" type="button">Exam issues</button>' +
        '<button class="options one" value="badges" type="button">Badge problems</button>'
        );
    })
  })
}

这就是我执行点击的方式

  // Pushes user's selected values as text chats
  $("#controls").on("click", ".options", function() {
    // Remove buttons on click
    $("#controls button.options").remove();
    var text = $(this).text();
    var b_val = $(this).val();

    // Set user's name in front of each message
    var userName = "<span class='username user-dialog dialog'> Me: </span>";
    var newMsg = text;

    var prevState = $("#conversation").html();
    if (prevState.length > 3) {
      prevState = prevState;
    }
    $("#conversation").html(
      prevState +
        "<div class='user-log'>" +
        "<span class='currentmsg user-dialog dialog' style='right:-40px; position:relative;'><span class='user-di-arrow'></span>" +
        newMsg +
        "</span> </div>"
    );
    // conditional codes
});

这也是我的fiddle。要查看我遇到的情况,请按以下步骤操作:

  1. 输入姓名
  2. 点击我的认证
  3. 点击实习问题
  4. 输入电子邮件
  5. 点击我的认证
  6. 编辑:here is a video of the problem

1 个答案:

答案 0 :(得分:0)

JSFiddle,您有

  if (email.length > 0 && email_confirmed == true) {
    $("#message").hide();
    send_msg(
      "One second, let me go check and see where your practicum is in the queue.",
      function() {
        send_msg(
          "It looks like your practicum will be graded by {{ DATE }}", function(){
          send_msg("If this seems too long we may be in a bit of a delay as our team grades all these by hand. ", function(){
            send_msg("If it is longer than 10 business days please post in our community as you may have submitted with another email address", function() {
            email_confirmed = false;
            $("#message").hide();
            **$("#controls").append(
            '<button class="options one" value="certifications" type="button">My Certifications</button>'
            );**
            })
          })
          })
        });
      }

完成询问后,它会调用

$("#controls").append(
                '<button class="options one" value="certifications" type="button">My Certifications</button>'
                );

以便依次调用相同的函数两次。你想删除。如果您不希望它返回并重新开始。

我希望这会有所帮助。