问题
I have a jquery slider, its like on default it is closed. So when the user try to open and ajax request is send and the data comes. My problem is each time the user closes and opens the ajax request is send. so how can i make the ajax call for the first time and if the user closes and opens the slider how to retain the data?
回答1:
You can set a variable, for example:
var alreadySent = false;
then in your ajax callback function you can change it to true (assuming you are using jQuery post):
if (!alreadySent) {
$.post("address", params, function(data) {
// do something
alreadySent = true;
});
}
回答2:
A wild guess: (I am a Javascript beginner, so this probably is a dumb idea)
You could add a class (lets call it '.clicked') to the "open link" in the ajax success function once the content has been loaded.
And than you would of course need to wrap the whole ajax code in an "if" statement to check if the link has already been clicked once and only execute the code if it hasnt been clicked and if there is no ".clicked" class applied to it yet.
I hope you get what I mean.
来源:https://stackoverflow.com/questions/7610918/slider-with-ajax-content