2013-02-19 73 views
0

这里是一个javascript将在edit-panes-delivery-delivery-country选项列表更改后运行,几个选项将追加到$(“select [id^=' edit-panes-delivery-delivery-zone']“),但ajax也会在edit-panes-delivery-delivery-country选项列表发生变化时运行,所以ajax动作将会覆盖javascript的结果。延迟javascript函数的切换时间

(function ($) { 
    // Original JavaScript code. 
    $('#edit-panes-delivery-delivery-country').ready (function() { 
     $("#edit-panes-delivery-delivery-country").change(function() { 
      if ($("#edit-panes-delivery-delivery-country option[value='156']").attr('selected')) { 

       var myOptions = { 
        949 : '福州 (hkpost only)', 
        951 : '廣州 (hkpost only)',    
        952 : '昆明 (hkpost only)', 
        956 : '溫州 (hkpost only)', 
        957 : '廈門 (hkpost only)' 

       }; 
       $.each(myOptions, function(val, text) { 
        //alert("added"); 
        $("select[id^='edit-panes-delivery-delivery-zone']").append(new Option(text,val)); 


       }) 
      } 
     }) 
    }) 
})(jQuery); 

所以,我的问题是,是否有可能推迟了jQuery例如几秒钟运行?

ps。该脚本运行在drupal 7模块中,因为我不知道在drupal 7中改变ajax动作,所以我选择延迟jquery运行

+0

您可以随时使用'的setTimeout()'。 – ATOzTOA 2013-02-19 02:37:47

+0

你看过'延迟()' - http://stackoverflow.com/questions/8401308/jquery-delay-function – 2013-02-19 02:39:41

回答

0

试试这个:

$("#edit-panes-delivery-delivery-country").change(function() { 
    setTimeout(myHandler, 5000); 
}); 

function myHandler(){ 
    // do everything here 
} 
0

将你的代码移动到一个单独的JS函数中,并用setTimeout在jQuery代码中,替换alert(“Hello”)。本例中的计时器为3000毫秒或3秒。

setTimeout(yourFunctionNamehere,3000);