• 0

[jQuery] .ajaxStop callback function being executed multiple times


Question

Hello everyone,

I'm using jQuery but my problem is that my page variable is being incremented several times even when I'm using "page += 1" in the .ajaxStop callback function because it's being executed more than once after the first time it's used. I use that variable as a parameter passed to the Flickr API to get a specific page of data.

What is happening is that the first time that function is called, the callback function is executed once. I then call the same function from a "more" button to get the next set of results but that time the function is called twice, the next time it's called thrice, and so on... That means that I can get page 1, 2, 4, 7, 11, etc...

The AJAX functions I'm calling are basically the .getJSON function and some extra .getJSON functions called in its callback method [inside getPhotos(id)]

// This gets the user ID from a given Flickr user page URL
function getUserID() {
??? $("#moreRow").hide(350);

??? var usr = document.getElementById('user').value
??? var Req_addr = 'http://api.flickr.com/services/rest/?method=flickr.urls.lookupUser&api_key=' + API_key + '&url=http%3A%2F%2Fflickr.com%2Fphotos%2F' + usr + json
??? $.getJSON(Req_addr, function(data) {
??? ??? // Once the user is known, data about its photos is requested??? 
??? ??? getPhotos(data.user.id)
??? });

??? // This hides the user data panel??? 
??? $("#userInfo").hide(0);

??? // This hides the settings panel??? 
??? $("#settings").hide(0, function() {
??? ??? $("#loader").slideDown(750);
??? });??? 

??? // This is what displays the photos when all of the AJAX requests have received their responses (ajaxStop)
?? ?$("#photos").ajaxStop(function() {
??? ??? // the page counter is incremented for the next page to be requested next time
??? ??? page += 1
??? ??? 
??? ??? // Add the data for the newly obtained photos to the table
??? ??? addPhotosToTable()
??? });
}


Any hint as to what I'm doing wrong?

You can see the whole source here: http://luisargote.com/javascript/argote_flickr.js

2 answers to this question

Recommended Posts

  • 0
  • 0
  On 23/11/2010 at 12:09, Argote said:

Well, I got the answer from StackOverflow in case anyone runs into this later: http://stackoverflow.com/questions/4254891/jquery-ajaxstop-callback-function-being-executed-multiple-times

I came across a similar issue the other day. I was accidentally binding my events every time an ajax request was made! So when I clicked a button for example the code was executed several times resulting in some very strange results! I was tearing my hair out over it, though it was a serverside issue :laugh:

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • The look speaks volumes, doesn't it?  
    • Whatever happened to offering a desirable product is what lead to profit?
    • 2 articles down "more gamers are switching to Windows 11, according to Valve."
    • After 4 years, the w11 adoption should be huge, but thats not the case, you see adoption dropping in another post. there are so many valid reason why people stick on w10, microsoft need to stop the whole support end at october. XP and 7 had extended support until a good version of windows released that people moved on to. right now w11 its a mess, its unreliable, unstable, buggy and the forced usability downgrades. Now more than ever people have alternatives even now M$ have a version with support until 2027 and 2032. its not that hard to keep supporting 10 until then. Hopefully by then w12 release and its a good option
    • Data (Use and Access) Bill faces pushback over AI and creative rights in UK parliament by Fiza Ali Image via: BBC In recent months, a series of high‐profile controversies has highlighted growing tension between technology platforms, content creators, and regulators over how artificial intelligence is trained and used. Previously, LinkedIn admitted sharing private messages to train its models, Meta was sued in France for using copyright‐protected content without authorisation, and OpenAI is paradoxically urging US lawmakers to expand "fair use" for AI. Meanwhile, several leading publishers, among them The New York Times and The Guardian, launched a "Support Responsible AI" campaign, accusing Big Tech of profiting from unlicensed journalism. Against this backdrop, the UK’s Data (Use and Access) Bill, which would allow AI firms to use all published content unless rights-holders opt out, has stalled in Parliament, with over 300 peers demanding AI companies disclose and license any copyrighted material they employ. Baroness Beeban Kidron has proposed an amendment requiring the Secretary of State for Science, Innovation, and Technology to report on the bill’s impact on the creative industries three months after it becomes law, unless the bill is amended first. Supporters of the government’s position, including Sir Nick Clegg, former president of global affairs at Meta, argue that requiring individual licences from all rights-holders would impose an unmanageable administrative burden and could deter AI investment in the UK. They say a broad opt-out regime is necessary for innovation. Opponents counter that unlicensed use of creative content risks harming writers, musicians, filmmakers, and other creators by allowing AI tools to produce similar work without compensation. Prominent artists such as Sir Elton John, Sir Paul McCartney, and Dua Lipa have warned that unrestricted access to their work could undermine their income and legacy. The debate traces back to early AI development, when large technology firms scraped substantial amounts of text, images, and other media from the internet without paying rights-holders. In 2022, the Ninth Circuit Court of Appeals held that "data scraping" does not constitute hacking, meaning it was not illegal to harvest publicly available website content so long as no defensive measures were bypassed. Those scraped datasets were then used to train generative models capable of producing text, images, and music in the styles of established creators. The Department for Science, Innovation, and Technology is conducting a broader consultation and will not amend the bill unless changes are deemed satisfactory for creators. If the bill cannot be agreed upon, it risks being withdrawn. The current impasse reflects a wider tension between supporting AI-driven innovation and protecting the economic interests of the creative industries. Until both Houses reach a compromise or one side concedes, the bill’s future remains uncertain.
  • Recent Achievements

    • Week One Done
      Epaminombas earned a badge
      Week One Done
    • Week One Done
      Prestige Podiatry Care earned a badge
      Week One Done
    • Week One Done
      rollconults earned a badge
      Week One Done
    • One Month Later
      lilred1938 earned a badge
      One Month Later
    • Week One Done
      lilred1938 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      134
    2. 2
      Xenon
      130
    3. 3
      ATLien_0
      123
    4. 4
      +Edouard
      102
    5. 5
      snowy owl
      95
  • Tell a friend

    Love Neowin? Tell a friend!