Can i store jQuery events in a variable?

Sojon

I want to store two or more events in a variable like element-selectors.
or - Is there any another way to do that? let me know.

The way I know:

let del = $('.delete');
let auto = $('.auto');
let show = $('.show');

del.fadeIn().delay(3000).fadeOut(function() {
  auto.fadeIn().delay(3000).fadeOut(function() {
    show.fadeIn().delay(3000).fadeOut();
  });
});
<div style="display: none" class="delete">delete</div>
<div style="display: none" class="auto">auto</div>
<div style="display: none" class="show">show</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

But I want something like this:

let envent = fadeIn().delay(3000).fadeOut(); 

del.envent {
  auto.envent {
    show.envent;
  };
};
T.J. Crowder

You do that by writing a reusable function:

function mySequence(el, callback) {
    return el.fadeIn().delay(3000).fadeOut(callback);
}

Then to use it, you pass del into it:

mySequence(del, function() {
    // Do the next thing
});

Live Example (with shorter delay):

function mySequence(el, callback) {
    return el.fadeIn().delay(300).fadeOut(callback);
}

let del = $('.delete');
let auto = $('.auto');
let show = $('.show');

mySequence(del, function() {
    mySequence(auto, function() {
        mySequence(show);
    });
});
<div style="display: none" class="delete">delete</div>
<div style="display: none" class="auto">auto</div>
<div style="display: none" class="show">show</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

You can also do a little jQuery plugin if you like:

$.fn.mySequence = function(callback) {
    return this.fadeIn().delay(3000).fadeOut(callback);
};

Then to use it, you use it like any other jQuery function:

del.mySequence(function() {
    // Do the next thing
});

Live Example (with shorter delay):

$.fn.mySequence = function(callback) {
    return this.fadeIn().delay(300).fadeOut(callback);
};

let del = $('.delete');
let auto = $('.auto');
let show = $('.show');

del.mySequence(function() {
    auto.mySequence(function() {
        show.mySequence();
    });
});
<div style="display: none" class="delete">delete</div>
<div style="display: none" class="auto">auto</div>
<div style="display: none" class="show">show</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


If you want to avoid directly passing callbacks in, you could use native Promises or jQuery's Deferred object. Here's a plugin that returns a Deferred's promise:

$.fn.mySequence = function() {
    var d = $.Deferred();
    this.fadeIn().delay(3000).fadeOut(d.resolve.bind(d));
    return d.promise();
};

Then to use it, you use it like any other jQuery function:

del.mySequence()
.then(function() {
    // Do the next thing
});

Live Example (with shorter delay):

$.fn.mySequence = function() {
    var d = $.Deferred();
    this.fadeIn().delay(300).fadeOut(d.resolve.bind(d));
    return d.promise();
};

let del = $('.delete');
let auto = $('.auto');
let show = $('.show');

del.mySequence()
.then(function() {
    return auto.mySequence();
})
.then(function() {
    return show.mySequence();
});
<div style="display: none" class="delete">delete</div>
<div style="display: none" class="auto">auto</div>
<div style="display: none" class="show">show</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can I find events bound on an element with jQuery?

How can I use jQuery for predefined events?

In java can i store a method in a variable?

Can I store the evaluation of a regex expression into a variable?

Can I store a composable function in a variable?

How can I store the result of a function into a variable?

How can I store dataset() in Application variable?

How can I store a variable in a list number?

How can I store a pattern in a variable in Rust?

How can I store my variable in a file?

Can I store a redirected url into a variable with javascript?

Can I store these selected filters in a variable?

How Can I Store a Wildcard in a Variable

Why can I not store user input into a variable and then use this variable?

How can I store a gameobject in a variable, destroy it in the scene, but not in the variable?

can i call stripe library if i store it as a string in a variable?

How can I order manipulated data that I store in a variable in SQL?

How can I use jQuery events and effects and DOM in javascript?

How can I proceed firing events after using stopImmediatePropagation() in jQuery?

How can I trigger the same function from multiple events with jQuery?

How can I reduce Jquery code with the same functionality, but different events

Should I store my jQuery selector(s) into a variable?

Store jquery selector in variable

How can I extract a value from a WMI object and store it in a variable?

Where can I store private key as environment variable in Mule?

How can I store a variable by passing it by reference in PHP?

How can I store a sql query into a variable, not the result?

Kibana: can I store "Time" as a variable and run a consecutive search?

Why I can not store the address of one variable in "Prolog+C"?