简体   繁体   English

Javascript-如何检查此事件是否已完成

[英]Javascript- How to check if operation has been completed on this event

Is there any way to check if the event is completed and element is free to perform another action? 有什么方法可以检查事件是否完成,元素是否可以执行其他操作? Like I want to do 就像我想做的

    $('#button-cancel').on('click', function() {
      // send ajax call
    });
    /****************************************
       extra code
    *******************************************/
$('#button-cancel').on('click', function() {
     if(ajax call is completed) {
       //do some thing  
    }
    });

I don't want to send ajax call in second onclick as it is already been sent, just want to check if it is done with ajax then do this 我不想在第二次onclick发送ajax调用,因为它已经发送过,只想检查它是否已用ajax完成,然后执行此操作

You can introduce a helper variable: 您可以引入一个辅助变量:

// introduce variable
var wasAjaxRun = false;
$('#button-cancel').on('click', function() {

    // in ajax complete event you change the value of variable:
    $.ajax({
        url: "yoururl"
        // other parameters
    }).done(function() {
        // your other handling logic
        wasAjaxRun = true;
    });
});

$('#button-cancel').on('click', function() {
     if(wasAjaxRun === true) {
       //do some thing  
    }
});

EDIT: I just noticed that you have event handlers attached to the same button. 编辑:我只是注意到您有事件处理程序附加到同一按钮。 In that case my initial answer would not work, because first event hander would be executed every time you click the button. 在这种情况下,我的初始答案将不起作用,因为每次单击按钮时都会执行第一事件处理程序。

It is not very clear from the description what you want to do with your first event hander. 从描述中不是很清楚您要如何处理第一个事件处理程序。 I assume you want to use some data, and if you already have this data, then you use it immediately (like in second handler), if you don't have it - you make the AJAX call to get the data (like in first handler). 我假设您要使用一些数据,并且如果您已经有了这些数据,那么您可以立即使用它(例如在第二个处理程序中),如果您没有它,则可以进行AJAX调用以获取数据(例如在第一个处理程序中)处理程序)。

For such scenario you could use single event handler with some conditions: 对于这种情况,您可以在某些条件下使用单个事件处理程序:

var isAjaxRunning = false;  // true only if AJAX call is in progress
var dataYouNeed;            // stores the data that you need

$('#button-cancel').on('click', function() {
    if(isAjaxRunning){
        return;    // if AJAX is in progress there is nothing we can do
    }

    // check if you already have the data, this assumes you data cannot be falsey
    if(dataYouNeed){  
        // You already have the data
        // perform the logic you had in your second event handler 
    }
    else {  // no data, you need to get it using AJAX
       isAjaxRunning = true;  // set the flag to prevent multiple AJAX calls
       $.ajax({
           url: "yoururl"
       }).done(function(result) {
           dataYouNeed = result;
       }).always(function(){
           isAjaxRunning = false;
       });
    }
});

You should be able to provide handlers for AJAX return codes. 您应该能够提供AJAX返回代码的处理程序。 eg 例如

$.ajax({
type: "post", url: "/SomeController/SomeAction",
success: function (data, text) {
    //...
},
error: function (request, status, error) {
    alert(request.responseText);
}
});

you can disable the button as soon as it enters in to the event and enable it back in ajax success or error method 您可以在按钮进入事件后立即将其禁用,并以ajax成功或错误方法将其启用

 $('#button-cancel').on('click', function() {
 // Disable button
 if(ajax call is completed) {
   //do some thing  
   //enable it back
}
});

This is edited, more complete version of dotnetums's answer, which looks like will only work once.. 这是经过编辑的dotnetums答案的更完整版本,看起来只能使用一次。

// introduce variable
var ajaxIsRunning = false;
$('#button').on('click', function() {

    // check state of variable, if running quit.
    if(ajaxIsRunning) return al("please wait, ajax is running..");
    // Else mark it to true
    ajaxIsRunning = true;

    // in ajax complete event you change the value of variable:
    $.ajax({
        url: "yoururl"
    }).done(function() {

        // Set it back to false so the button can be used again
        ajaxIsRunning = false;

    });
});

You just need to set a flag that indicates ajax call is underway, then clear it when ajax call returns. 您只需要设置一个标志,表明正在进行ajax调用,然后在ajax调用返回时将其清除。

 var ajaxProcessing = false; $('#button-cancel').on('click', function(){ processAjaxCall(); }); function processAjaxCall() { if(ajaxProcessing) return; ajaxProcessing = true; //set the flag $.ajax({ url: 'http://stackoverflow.com/questions/36506931/javascript-how-to-check-if-operation-has-been-completed-on-this-event' }) .done(function(resp){ //do something alert('success'); }) .fail(function(){ //handle error alert('error'); }) .always(function(){ ajaxprocessing = false; //clear the flag }) } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="button-cancel">Cancel</button> 

What you can do is call a function at the end of an if statement like 您可以做的是在if语句的末尾调用一个函数,例如

if(ajax call is completed) {
       checkDone();  
}

function checkDone() {

   alert("Done");

}

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

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