0

Here is a javascript will be ran after the change of edit-panes-delivery-delivery-country option list, several options will be append to the $("select[id^='edit-panes-delivery-delivery-zone']"), but a ajax will also be ran when the change of edit-panes-delivery-delivery-country option list, so the ajax action will cover the result of the 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);

So, my question is, is it possible to delay the jquery to run for example a few seconds?

ps. The script is run in drupal 7 module, because i don't know alter ajax action in drupal 7, so i choose to delay the jquery to run

hkguile
  • 4,235
  • 17
  • 68
  • 139

2 Answers2

0

Move your code into a separate JS function, and call it with a setTimeout inside the jQuery code, replacing alert("Hello"). The timer in this example is 3,000 milliseconds, or 3 seconds.

setTimeout(yourFunctionNamehere,3000);
Ripside
  • 145
  • 2
  • 8
0

Try this:

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

function myHandler(){
    // do everything here
}
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117