简体   繁体   中英

Jquery - Ajax - callback function?

I am using a $.get request to get some data and am then storing it in a cookie. The get request is ran when a user clicks a button. They would then follow a link to another page and the data stored to the cookie is extracted and displayed on the new page.

Unfortunately some people are rather vigorous with their clicking, the get request runs but the go to the next page before it's had time to complete. This obviously causes an error. So I am wanting to code something which will disable the link to the next page until the get request has finished, but I don't know how.

This is all I have at the moment.

$.get(url, function(d) {
                    cp = $('textarea#txt_quote', d).html();
                    z_cookie.set(topic_id, current_posts, '1');
                });

You can disable the link by handling its click event, like this:

var link = $('selector to find <a> tag');
link.click(function() { return false; });


$.get(url, function(d) {
    cp = $('textarea#txt_quote', d).html();
    z_cookie.set(topic_id, current_posts, '1');

    link.unbind('click');
});

If you want to, you can set a variable in the click handler, and, if it's been set, navigate to the page after the AJAX request finishes, like this:

location.href = link.attr('href');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM