2010-11-23 22 views
8

jQuery kullanıyorum ama benim sorunum, sayfa değişkeninin .ajaxStop geri arama işlevinde "page + = 1" kullanırken bile defalarca artırılıyor olmasıdır İlk kez kullanıldıktan sonra birden fazla kez çalıştırıldı. Belirli bir veri sayfası almak için bu değişkeni Flickr API'sine iletilen bir parametre olarak kullanıyorum..ajaxStop geri arama işlevi birden çok kez çalıştırılıyor

Neler olup bittiği, bu işlevin ilk çağrıldığı zaman, geri çağırma işlevi bir kez gerçekleştirilir. Daha sonra bir sonraki sonuç kümesini elde etmek için bir "daha fazla" düğmesinden aynı işlevi çağırıyorum ama bu kez işlev iki kez çağrılıyor, bir dahaki sefere üç kez çağrılıyor ... Yani sayfa 1'i alabileceğim anlamına geliyor, 2, 4, 7, 11, vs ...

aradım AJAX fonksiyonları

temelde .GetJSON fonksiyonu ve geri arama yöntemi olarak adlandırılan bazı ekstra .GetJSON fonksiyonları [getPhotos içine (id)] olan

// This gets the user ID from a given Flickr user page URL and does some presentation stuff 
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() 
    }); 
} 

Yanlış yaptığım şey hakkında bir ipucu?

Burada bütün kaynağını görebilirsiniz. http://luisargote.com/flickr/javascript/argote_flickr.js

+0

Ve burada webapp kullanmayı deneyebilirsiniz: o yardım sayesinde iyi çalışıyor ama – Argote

+0

Benim WebApp giderildi açıklanan sorunu nedeniyle bazı sayfaları atlar http://luisargote.com/flickr.php ! – Argote

cevap

10

Görmek, .ajaxStop()'un yeni olay işleyicisini eklediği ve her seferinde yeni bir tane eklediğinizden kaynaklanıyor. Sadece bu gibi (bir document.ready işleyicisi içinde ama yine de) dışına taşıyın:

// This gets the user ID from a given Flickr user page URL and does some presentation stuff 
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() 
}); 

alternatif (nedense #photos darmadağın alırsa) o fonksiyonu ve kullanım içinde olduğu yerde bırakabilir olduğunu böyle .one(): Bu istediğiniz etkiyi veren o unbind sonra, bir kez işleyicisi çalışır ... ama eleman yerde tahrip oluyor sürece (o öyle gibi gelmiyor)

$("#photos").one("ajaxStop", function() { 

ile sopa bir kez dışarı bağlama, sebep yok t ekstra iş yapmak.

+0

Çok teşekkür ederim, bu yüzden ajaxStop dediğimde temel olarak AJAX olayları çağrıldığında kalıcı bir dinleyici türü oluşturur? Ve bu dinleyici sadece #photos kapsamında çalışacak? – Argote

+1

@Argote - yup, bu öğe yok edilmedikçe, bu olay işleyicileri yok olur ve toplanır. Kapsam kısmında yok, “ajaxStop” küresel bir olaydır, her öğenin patladığı anda ateşler. –

+0

Çok teşekkür ederim, bunu yapmak için yeterince itibar kazandığımda cevabınızı yararlı olarak işaretlediğinizden emin olacağım. – Argote

0

Kontrolü $ ("# fotoğrafları") uzunluğu uzunluğu, sayfanız bu listedeki her öğe için arttırılmış olacak sen ne

+0

Bu ilginç bir teklif, neden bir kereden fazla çağrıldığını bilmek isterim. – Argote

1

Daha fazla ayrıntı isteğinde bulunduğunuz her sefer ajaxStop yeniden bağlayıcınızdır.

Olay bağlayıcısını getUserId dışına taşıyın ve sayfa yüklemesinde bir kez yapın.

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); 
    });  
} 

jQuery(document).ready(function ($) { 
    // 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() 
    }); 
}); 
+0

Teşekkür ederim Çok fazla, Matt ile çalışıyorum, jQuery (document) .ready (function ($) {window yerine çağrılan bir işlev yerine kullanıyorum.yükler, eğer öyleyse, neden? – Argote

+1

@Argote: 'jQuery (document) .ready()' işlevini kullanarak, birden çok işleyiciyi bağlamanıza izin verirken, 'window.onload' yalnızca bir tanesine izin verir (bu konuda zeki olmazsanız). – Matt