简体   繁体   English

我的jQuery函数加载太慢了吗?

[英]Is my jQuery function loading too slowly?

I built a contact form on my site with a "Send" button that will turn into "Sending..." when you click on it. 我在网站上建立了一个带有“发送”按钮的联系表单,当您单击该表单时,该表单将变为“正在发送...”。 I used Bootstrap JS for that. 我为此使用了Bootstrap JS。

It will reset when the

  1. fields are blank 字段为空
  2. the email address provided is not valid 提供的电子邮件地址无效
  3. something went wrong with the server 服务器出了点问题
  4. the form is sent 表格已发送

I have a div above the form where the alerts/warnings will appear. 我在警报/警告将出现的表格上方有一个div。 However, it only resets when the form is sent. 但是,仅在发送表单时重置。 The button stays at "Sending..." for the other 3 situations. 对于其他3种情况,按钮保持在“正在发送...”。

I thought it was because the button.('reset') wasn't even called, but I put button.('loading') where button.('reset') was supposed to be and it worked (removed the original button.('loading') to work). 我以为是因为甚至没有调用button。('reset'),但是我将button。('loading')放在了button。('reset')应该存在的地方,并且它起作用了(删除了原始按钮。 (“加载”)正常工作)。

I use JS for the first 3 situations and AJAX for the last one. 我在前三种情况下使用JS,在后一种情况下使用AJAX。 I couldn't locate the error here, so I have a suspicion that it could be because the JS (for the alert/warning to appear) loaded faster than the jQuery (for button reset). 我无法在此处找到错误,因此我怀疑这可能是因为JS(用于显示警告/警告)的加载速度比jQuery(用于按钮重置)的加载速度更快。 The form takes about a few seconds to send and when it does, the button resets diligently. 该表格大约需要几秒钟的时间发送,当它发送时,按钮会被认真地重置。 If there is an error, the warning message pops up immediately, almost faster than triggering the "Sending..." button. 如果出现错误,警告消息将立即弹出,几乎比触发“正在发送...”按钮更快。

I've tried looking at the timeline tab in the dev tools section, but I don't really understand what I'm looking at (which box refers to which action?) 我尝试查看“开发工具”部分中的“时间轴”选项卡,但是我并不太了解我在看什么(哪个框代表哪个操作?)

Now I use a setTimeout function to reset the button and it works, but I wish to reset it the "proper" way (button resets only after the warning appears). 现在,我使用setTimeout函数来重置按钮,并且它可以工作,但是我希望以“适当”的方式重置它(仅在警告出现后重置按钮)。

Can someone guide me through this? 有人可以指导我吗? Thank you so much! 非常感谢!

Sorry, here are the relevant codes: 抱歉,这是相关代码:

// scripts.js

$("#bugButton").on('click', function() {
    $("#bugButton").button('loading');  
})

function bugReport(bugsField, emailField) {
    if (bugsField.value != '' && emailField.value != '') {
        if (validateEmail(emailField.value) == true) {
            params = "bugsField=" + bugsField.value + "&emailField=" + emailField.value
            request = new ajaxRequest()
            request.open('POST', 'mail.php', true)
            request.setRequestHeader('Content-type','application/x-www-form-urlencoded')

            request.onreadystatechange = function() {
                if (this.readyState == 4) {
                    if (this.status == 200) {
                        if (this.responseText != null) {
                            alertGen(this.responseText)
            } } } } 
            request.send(params) 
        } else
            alertGen('errmail');
    } else
        alertGen('errblank');
}

function alertGen(message) {
    msg1 = '<div class="alert alert-'
    msg2 = ' alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'

    if (message == 'success') 
        document.getElementById('response-box').innerHTML = msg1 + 'success' + msg2 + 'Bug report sent!</div>'
    else if (message == 'errblank')
        document.getElementById('response-box').innerHTML = msg1 + 'info' + msg2 + 'Field(s) are blank.</div>'
    else if (message == 'errsend')
        document.getElementById('response-box').innerHTML = msg1 + 'danger' + msg2 + 'Something went wrong. :-(</div>'
    else if (message == 'errmail') 
        document.getElementById('response-box').innerHTML = msg1 + 'danger' + msg2 + 'That is not a valid email address.</div>'
    else 
        document.getElementById('response-box').innerHTML = msg1 + 'info' + msg2 + message + '</div>';

    setTimeout(function () {
        $("#bugButton").button('reset');
    }, 10);
}





// index.html

<div class="modal fade" id="bugsModal" tabindex="-1" role="dialog" aria-labelledby="bugsModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      </div>
      <div class="modal-body">
        <div id="response-box" onchange="resetButton()"></div>
        <h3>Report a bug</h3><br>
        <form method="POST">
            <div class="form-group">
                <textarea type="text" class="form-control bugsField" placeholder="What seems to be the problem?" rows="5" maxlength="1000" aria-describedby="bugsField" name="bugsField" value="<?php echo isset($_POST['bugs']) ? $_POST['bugs'] : '' ?>"></textarea>
            </div>
            <div class="form-group">
                <input type="text" class="form-control" placeholder="Email" maxlength="75" aria-describedby="emailField" name="emailField" value="<?php echo isset($_POST['email']) ? $_POST['email'] : '' ?>">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" id="bugButton" data-loading-text="<i class='fa fa-refresh fa-spin'></i>" onClick="bugReport(bugsField, emailField)" autocomplete="off">Send</button>
              </div>
        </form>

      </div>
    </div>
  </div>
</div>

With out coding,it will not help us to identify the Error. 如果不进行编码,将无法帮助我们识别错误。

But check for these error 但是检查这些错误

  1. whether other three event after finished going for the coding part which is changing the button name. 完成更改按钮名称的编码部分后是否还有其他三个事件。
  2. Any name changed in other three events. 在其他三个事件中,任何名称都已更改。
  3. As you have changed the name of button id or class[I am assuming here] as it worked ,check with different name as well as some time same name in other coding part or other js file will make the code to not work. 当您更改了按钮ID或类的名称(我在这里假设)后,请使用其他名称以及其他编码部分或其他js文件中的相同名称进行检查,这将使代码无法正常工作。
  4. Finally keep alert message box or console message or exception handing to see any error or whether the coding is getting executed . 最后,保留警报消息框或控制台消息或异常处理,以查看任何错误或代码是否正在执行。

I have seen the code try placing the $("#bugButton").button('reset'); 我已经看到代码尝试放置$(“#bugButton”)。button('reset'); at the start of alertGen() function and try.if not working please tell me I will see. 在alertGen()函数的开始并尝试。如果不起作用,请告诉我,我会看到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM