1

I have a loader,

<div id="loading">Loading..!</div>
<script>$('#loading').hide();</script>

and i want to show that loader automatically when a js function is started and hide when js function completed.

and in js file,

function myfunc{
$('#loading').show();

//some bla bla;

$('#loading').hide();
}

function myfunc1{
$('#loading').show();

//some bla bla;

$('#loading').hide();
}

.... .. ....

function myfuncN{
$('#loading').show();

//some bla bla;

$('#loading').hide();
}

i want to show loader automatically instead of writing that statement to every function.

2
  • I would say you're doing it the correct way. This way is clear about what is happening, and it's not all that inconvenient to have two important lines. Working out some function detection thing to execute one line is a lot of potentially complex work for almost no gain. Commented Jun 21, 2018 at 15:25
  • Do your functions use async/await? If so please indicate in your examples. Otherwise, if they execute synchronously, there's no point in trying to show and hide the loader because the browser would not repaint until the function completes, and you would never see the loader. Commented Jun 21, 2018 at 15:25

3 Answers 3

1

As already mentioned in comments, you're following more clear way now, but if you still wanting to apply additional lines to existing functions, you can use .apply() for that like I did it in applier function below:

function applier(initialFunction) {
    return function() {
        alert('apllied part before main logic');
        var result = initialFunction.apply(this, arguments);
        alert('apllied part after main logic');
    };
}

func1 = function() {
    alert('func1, logic from main function');
}
func2 = function() {
    alert('func2, logic from main function');
}

var newFunc1 = applier(func1);
var newFunc2 = applier(func2);

newFunc1();
newFunc2();

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using ajax jQuery function, you can declare a global Ajax configuration like this

$.ajaxSetup({
    beforeSend: function() {
        $('#loading').show();
    },
    complete: function() {
        $('#loading').hide();
    }
});

This way you can show your loading indicator on every time an asyncronus call starts and hide it when it ends

I hope this Helps

Comments

0

You can write a function to decorate other functions.

in ES6

function showAndHide(func) {
  return (...args) => {
    $('#loading').show();

    func(...args);

    $('#loading').hide();
  };
}

const myFunc = showAndHide(() => {
   //blah blah
});

or is ES5

function showAndHide(func) {
  return function decoratedShowAndHide() {
    $('#loading').show();

    func.apply(this, arguments);

    $('#loading').hide();
  };
}

var myFunc = showAndHide(function () {
   //blah blah
});

The showAndHide function accepts a function as an argument and returns a function. It can thus decorate the execution of the function and provide reuse.

1 Comment

The other concerns about if these show and hide are wrapping async actions and whether this code would work at all in a synchronous execution are valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.