exit/break main function from called/child function

max

I'm trying to create something like button loading in bootstrap. basically creating a loading text inside a button/link and preventing button/link from functioning until the end of operation

So here is my prototype

function btnLoader(b){
    var txt = b.attr('data-text' , b.text());
    b.addClass('disabled').text('......');
}

function btnunLoader(b){
    b.removeClass('disabled').text( b.attr('data-text'));
}

So this works fine like

<a href="#" onclick="mainFunction(1);" id="btn-1"> send comment </a>
<script>
function mainFunction( id){
    btnLoader($('#btn-'+id));
    do stuff 
    btnunLoader($('#btn-'+id));
}
</script>

or

<a href="#" id="like-btn"> LIKE </a>
<script>
$('#like-btn').on('click'  , function(){
    btnLoader($(this));
    do stuff        
})
</script>

Here is the problem: I want the button to be disabled after the loading

I can do something like this to check if button already has disabled class

function btnLoader(b){

    if(b.hasClass('disabled'))
        return false;

    var txt = b.attr('data-text' , b.text());
    b.addClass('disabled').text('......');
}

But it has no effect on the mainFunction which is calling the btnLoader function or on the second example i want to break the click event from btnLoader

Is there anyway to do that ?

Pleas note that clicked element might be <a> or any other tag and they don't have a disabled attribute so that's not an option

xpy

Looks like a callback to me. I suppose a jQuery function will be ok.

$.fn.btnLoader = function(callback) {
  this.on('click', function() {
    btnLoader.call(this, callback);
  });
};

function btnLoader(callback) {


  if ($(this).is('.disabled'))
    return;
  
  console.log("doing btnLoader stuff");
  var txt = $(this).attr('data-text', $(this).text());
  $(this).addClass('disabled').text('......');
  callback.call($(this));
}


function mainFunction() {

  console.log("doing stuff");
  // do stuff
  btnunLoader(this);
}


$('#like-btn').btnLoader(function() {
  console.log("doing other stuff");
  // do stuff 
});

function btnunLoader() {
  $(this).removeClass('disabled').text($(this).attr('data-text'));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" onclick="btnunLoader.call(this,mainFunction)" id="btn-1"> send comment </a>
<br>
<a href="#" id="like-btn"> LIKE </a>

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Golang edit array of struts from main() to function

分類Dev

Moving pointer from main() to first executable function

分類Dev

Exit main function from inside forEach loop inside nested function

分類Dev

Python - Calling function from main() function returns zero

分類Dev

Main is usually a function?

分類Dev

SetLayout Function main purpose

分類Dev

Calling function in a C++ source file from main

分類Dev

how to remove rows and cols from main matrix after SVD function?

分類Dev

Pass variable to main function Nodejs

分類Dev

Calling a function from within a function

分類Dev

How to run a separate function not included in if __name__=="__main__" from the command line?

分類Dev

Why would PHP included via a function return a different result from the PHP in the main file?

分類Dev

get main function name when nested function is running - in python

分類Dev

<function __main __。add_numbers(x、y)>の__main__

分類Dev

Python command line call to main does not go into main function

分類Dev

Display alert on main page when function is called

分類Dev

Bad file number in simple main function with pipes

分類Dev

C++ - Friend function unable to be called in main?

分類Dev

Stack memory operations at the beginning of main function in assembly

分類Dev

How to process return value of main() function?

分類Dev

PowerShell refactoring functions to one main function

分類Dev

C function error LNK2019 in main

分類Dev

Access variable from function

分類Dev

scanf from function to array

分類Dev

Get struct from function

分類Dev

Execute function from ollydbg?

分類Dev

Stop Javascript Function execution from another Function

分類Dev

Custom print function yield from sub function

分類Dev

C++ exit function from another function

Related 関連記事

  1. 1

    Golang edit array of struts from main() to function

  2. 2

    Moving pointer from main() to first executable function

  3. 3

    Exit main function from inside forEach loop inside nested function

  4. 4

    Python - Calling function from main() function returns zero

  5. 5

    Main is usually a function?

  6. 6

    SetLayout Function main purpose

  7. 7

    Calling function in a C++ source file from main

  8. 8

    how to remove rows and cols from main matrix after SVD function?

  9. 9

    Pass variable to main function Nodejs

  10. 10

    Calling a function from within a function

  11. 11

    How to run a separate function not included in if __name__=="__main__" from the command line?

  12. 12

    Why would PHP included via a function return a different result from the PHP in the main file?

  13. 13

    get main function name when nested function is running - in python

  14. 14

    <function __main __。add_numbers(x、y)>の__main__

  15. 15

    Python command line call to main does not go into main function

  16. 16

    Display alert on main page when function is called

  17. 17

    Bad file number in simple main function with pipes

  18. 18

    C++ - Friend function unable to be called in main?

  19. 19

    Stack memory operations at the beginning of main function in assembly

  20. 20

    How to process return value of main() function?

  21. 21

    PowerShell refactoring functions to one main function

  22. 22

    C function error LNK2019 in main

  23. 23

    Access variable from function

  24. 24

    scanf from function to array

  25. 25

    Get struct from function

  26. 26

    Execute function from ollydbg?

  27. 27

    Stop Javascript Function execution from another Function

  28. 28

    Custom print function yield from sub function

  29. 29

    C++ exit function from another function

ホットタグ

アーカイブ