使用jquery的.queue对函数进行排队

时间:2010-11-15 15:44:11

标签: javascript jquery queue

当我从.queue()函数中删除参数'ajax'时,我的ajax调用会排队。唯一的问题是,jQuery文档说.queue()函数将默认为'fx'队列。不幸的是,我已经在使用这个队列(用于效果),我想专门为自定义函数使用另一个队列。不幸的是,.queue()函数中的代码永远不会被调用。我有一个下面的代码示例(只是一个小片段)。如果您有其他问题可以随意发表评论,这会变得有点复杂。

$(document).ready(function(event) {

var target = event.target;
var ajaxify = new Ajaxify();

$.each(ajaxify.functions, function(index, value){

     if ($(target).hasClass(value)) {        
       console.log('this is in my console, and nowhere else in my code');
       $('#main').queue('customfunctions', function (next) {
         var self = this;          
         ajaxify[value](target, event, next, self);       
       });

     }

   });
  $('#main').dequeue('customfunctions');
});

function Ajaxify() {

  this.functions = [
                   'ajaxify_overlay',
                   'ajaxify_overlayCancel',
                   'ajaxify_overlaySubmit',
                   'ajaxify_rollout',
                   'ajaxify_rolloutCancel',
                   'ajaxify_rolloutSubmit',
                   'ajaxify_upload',
                   'ajaxify_contentArea',
                   'ajaxify_itemToggler',
                   'ajaxify_closer',
                   'ajaxify_submit',
                   'ajaxify_inputActivate',
                   'ajaxify_executeAndRefresh',
                   'ajaxify_empty' //no comma on the last entry!!!!  
                 ];

}

Ajaxify.prototype.ajaxify_executeAndRefresh = function (target, event, next, self) {

  event.preventDefault();

  var newPath = getVar($(target).attr('class'), 'url'); //getVar function not included, it will get the new path for the ajax call below

  var url = $(target).attr('href');

  $.ajax({    
    type: "POST",
    url: url,
    success: function(transport) {

      refreshPage(newPath); //refreshPage function not included, it will do a page refresh with the new path

      next();     

    }
  });

}

1 个答案:

答案 0 :(得分:3)

您需要调用dequeue()来运行队列中的下一个函数。