2013-02-26 66 views
3

我正在閱讀關於jquery queue()here。第一個答案是非常全面:使用jquery next()在這個隊列中的功能

var theQueue = $({}); // jQuery on an empty object - a perfect queue holder 

$.each([1,2,3],function(i, num) { 
    // lets add some really simple functions to a queue: 
    theQueue.queue('alerts', function(next) { 
    // show something, and if they hit "yes", run the next function. 
    if (confirm('index:'+i+' = '+num+'\nRun the next function?')) { 
     next(); 
    } 
    }); 
}); 

// create a button to run the queue: 
$("<button>", { 
    text: 'Run Queue', 
    click: function() { 
    theQueue.dequeue('alerts'); 
    } 
}).appendTo('body'); 

// create a button to show the length: 
$("<button>", { 
    text: 'Show Length', 
    click: function() { 
    alert(theQueue.queue('alerts').length); 
    } 
}).appendTo('body'); 

jsfiddle

我不明白怎麼next()(8號線),然而工作。我得到的是,如果我註釋掉「next」確認框不能彈出,直到我再次按下按鈕,但我認爲next()主要用於遍歷dom。儘管如此,它似乎正在告訴函數再次運行。

此外,如果confirm框的排隊功能位於each()功能範圍內,爲什麼它們都會在沒有next()的情況下成功排隊?

回答

3

next是回調函數中的一個參數來排隊。 請勿將.next()混淆,這是用於DOM導航的jQuery收集方法。

實際上,您可以在回調as I demonstrate here中設置所需的參數。名稱next不是特別重要;它只是在文檔中有意義。

該參數本身是一個函數(回調函數),它由jQuery的內部函數傳遞給.queue

+0

哈哈,好的謝謝:) – 1252748 2013-02-26 06:39:32