Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Help removing ‘click’ function—fade out element

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #36026
    Rugg
    Participant

    Hello All,

    Below is a snippet of jQuery including a click and timer function. The user can click the specified element and it will fade away—or the element will automatically fade away according to the specified timer, if not clicked.

    My goal is to remove the clickable aspect of this—resulting in a timer only based solution to fading the element out.

    I am new to JS so please bear with me. Thank You.

    $(document).ready(function(){
    jQuery.fn.fadeToggle = function(speed, easing, callback) {
    return this.animate({opacity: 'toggle'}, speed, easing, callback);
    };

    $("#load").click(function() {
    $("#load").fadeOut(800);
    });

    var tmr = setTimeout(function() {
    clearTimeout(tmr);
    $('#load').trigger('click');
    }, 800);
    });
    #94317
    jamygolden
    Member

    Try this:

    $(document).ready(function(){
    jQuery.fn.fadeToggle = function(speed, easing, callback) {
    return this.animate({opacity: 'toggle'}, speed, easing, callback);
    };

    var tmr = setTimeout(function() {
    clearTimeout(tmr);
    $("#load").fadeOut(800);
    }, 800);
    });
    #94321
    SgtLegend
    Member

    You don’t even need to use the setTimeout function as jQuery has a built in method called delay() which allows you to set an inline timer using just one line of code, see the below example:

    $(function() {
    $('#load').delay(800).fadeOut(800);
    });

    Also when using the setTimeout function there is no need to clear the timeout as it only runs once when the DOM loads.

    #94364
    jamygolden
    Member

    @NSR $(function() { is short for $(document).ready(function() {

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.