tags:

views:

182

answers:

3

I have this this jquery that loads a success message in a modal:

$('#menu_access').load(
    site_url + sUrl,
    function() {
        $(this).dolPopup(oPopupOptions);
    }
);

I want to call this function three seconds after the load:

$('#menu_access').dolPopupHide({});

I'm not sure how to go about it. I'm not too familiar with the .load() function.

+3  A: 

Use the .delay() function.

JoshRoss
+3  A: 

Use a call to setTimeout(...)

$('#menu_access').load(
    site_url + sUrl,
    function() {
        $(this).dolPopup(oPopupOptions);
        setTimeout(function () {
            $('#menu_access').dolPopupHide({});
        }, 3000);
    }
);
Dan Herbert
hmm... not working for some reason. This is being called from inside a function
whatshakin
`this` isn't the proper element inside the function passed to `setTimeout()` here.
Nick Craver
@Nick You're right. I don't know how I missed that. I fixed it. Thanks!
Dan Herbert
Then after this edit, It works perfect. Thanks Everyone for the help.
whatshakin
+8  A: 

You can use jQuery's delay() method, but you need to add your plugin call to the queue using jQuery's .queue() method.

$('#menu_access').load(
    site_url + sUrl,
    function() {
        $(this).dolPopup(oPopupOptions);
        $(this).delay(3000)
               .queue( function( nxt ) {
                      $(this).dolPopupHide({});
                      nxt();
                });
    }
);

Note that the parameter to the function passed to .queue() needs to be called in order to allow the next animation to occur (at any time).

As an alternative, you can use setTimeout(), but you need to make sure the value of this is correct in the callback.

$('#menu_access').load(
    site_url + sUrl,
    function() {
        $(this).dolPopup(oPopupOptions);
        var that = this;
        setTimeout(function() {
                      $(that).dolPopupHide({});
                }, 3000);
    }
);

Here this is referenced by the variable that.

Or you could use jQuery's $.proxy() method to ensure the proper meaning of this.

$('#menu_access').load(
    site_url + sUrl,
    function() {
        $(this).dolPopup(oPopupOptions);
        setTimeout($.proxy(function() {
                      $(this).dolPopupHide({});
                }, this), 3000);
    }
);
patrick dw
where do I add $(this).dolPopupHide({});?
whatshakin
@what - Sorry, I should have had that in where the `.dolPopup(oPopupOptions)` is. I'll correct.
patrick dw
+1 - Complete and correct, curious how the other answers got +3, one's inefficient and one's wrong....`setTimeout()` is *definitely* the way to go here.
Nick Craver
@Nick - Agreed. I don't see any real benefit to `.delay(ms).queue(fn)`. Extra overhead, and the code isn't any simpler/shorter/more compatible.
patrick dw
Good Stuff. I'm working inside of a large script that has alot of js problems. It was the close function i was trying to use that was failing me. So I just added some .remove() 's and setTimeout() works like a charm.
whatshakin
@what - Glad it worked. In my opinion, it would be much better to use some reference to `this` in the `setTimeout()` instead of selecting it again from the DOM using `"#menu_access"`. You have a direct reference to the element. Might as well use it. :o)
patrick dw