最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - setTimeout() doesn't seem to be working in Firefox? - Stack Overflow

programmeradmin2浏览0评论

I have inherited a website! which was designed to work in IE and only IE it seems.. I've now been asked to make the site run in Firefox. I've fixed most of the bugs without any problems but this one has me stumped.

setTimeout(fDelayedFunc, 1000);

This line of Javascript, works fine in IE but in Firefox the function fDelayedFunc never fires. I've removed the setTimeout and the function wrapper and tried running the code as part of the main function. This works without any problems at all.

There is alot of code involved but here's the main but I'm having trouble with. If you'd like to see anymore of the code please let me know.

            setTimeout(fDelayedFunc, 0);

            //Save the current text box value
            var vCurrentTBValue = vJQElement.val();

            function fDelayedFunc() {

                if (vJQElement.val() == vCurrentTBValue) {
                    alert("test");

                    //Get position list box should appear in
                    var vTop = vJQElement.position().top + 25;
                    var vLeft = vJQElement.position().left;

                    //Had to put a special case in for account due to the position co-ords being wrong. This is due to a css error
                    if (vHiddenFieldToWriteTo == "#ctl00_ContentPlaceHolder1_hfAccountCode") {
                        vTop = vJQElement.position().top + 58;
                        vLeft = vJQElement.position().left + 200;
                    }
                    else {
                        vTop = vJQElement.position().top + 25;
                        vLeft = vJQElement.position().left;
                    }


                    //Create div element
                    var vDivElement = $("<div id='divSearchBox' style='position:absolute; top:" + vTop + ";left:" + vLeft + "; z-index: 40000;'></div>");

                    //Create list box
                    var vListBox = $("<select id='lbResults' tabIndex='" + vJQElement.attr("tabIndex") + "' size='4' style='height:400px;'></select>");


                    //Bind a function to the list box which will select the item via either tab or enter
                    vListBox.bind("keydown", function() {
                        //Check if tab or enter has been pressed
                        if (event.keyCode == 9 || event.keyCode == 13) {
                            //Set hidden value to the selected items code
                            $(vHiddenFieldToWriteTo).val($(vListBox.find(":selected")).val());

                            //Create postback
                            $('#ctl00_ContentPlaceHolder1_wizNewConsignment_btnRefresh').click();
                        }
                        //Check if the up arrow has been pressed at the top of the listbox
                        else if (event.keyCode == 38 && $(vListBox.find(":selected")).val() == $(vListBox.find(":first")).val()) {
                            //Focus back on the search box
                            vElement.focus();
                        }
                    }).bind("dblclick", function() {
                        //Set hidden value to the selected items code
                        $(vHiddenFieldToWriteTo).val($(vListBox.find(":selected")).val());

                        //Create postback
                        $('#ctl00_ContentPlaceHolder1_wizNewConsignment_btnRefresh').click();
                    });

                    //Get search field
                    var vSearchText = vJQElement.val();

                    var vDepotID = $("#ctl00_ContentPlaceHolder1_wizNewConsignment_hfDepotID").val();
                    var vCustomerID = $("#ctl00_ContentPlaceHolder1_wizNewConsignment_hfCustomerID").val();
                    var vCountryID = $("#ctl00_ContentPlaceHolder1_wizNewConsignment_hfCountryID").val();
                    var vConsignee = vJQElement.attr("boolConsignee");

                    //Set a loading image in place until call pleted
                    vJQElement.css("backgroundImage", "url(images/small-loader.gif)");
                    vJQElement.css("backgroundRepeat", "no-repeat");
                    vJQElement.css("backgroundPosition", "right");





                    //Make AJAX call
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "NewConsignment.asmx/fGetAddressesAndIDs",
                        data: "{'strSearchText' : '" + vSearchText + "', 'intDepotID' : '" + vDepotID + "', 'intCustomerID' : '" + vCustomerID + "', 'intCountryID' : '" + vCountryID + "', 'boolConsignee' : '" + vConsignee + "'}",
                        dataType: "json",
                        success: function fGetAddressesAndIDsResult(GetAddressesAndIDsResult) {
                            //Make sure there are results
                            if (GetAddressesAndIDsResult != null && GetAddressesAndIDsResult != "") {
                                var vNumberOfResults = 0;
                                var vNumberOfLearntAddresses = 0;
                                var vLearntAddressUniqueID = "";

                                //Try to get results (first call will work on Linux and catch will work on Windows)
                                try {
                                    //Check array exists (if this fails will go to catch)
                                    if (GetAddressesAndIDsResult.d.length > 0) {
                                        //Loop through the results
                                        $.each(GetAddressesAndIDsResult.d, function() {
                                            //Check for results
                                            if (this.length > 0) {
                                                //Evaluate JSON
                                                var vAddress = eval("(" + this + ")");

                                                //Create list item
                                                var vOption = $("<option class='AddressOption' value='" + vAddress.uniqueID + "'>" + vAddress.briefDescription + "</option>");

                                                //Find out number of learnt addresses
                                                if (vAddress.uniqueID.indexOf("ConLA") != -1) {
                                                    vNumberOfLearntAddresses++;
                                                    vLearntAddressUniqueID = vAddress.uniqueID;
                                                }

                                                //Add list item to list box
                                                vListBox.append(vOption);

                                                //Mark result added
                                                vNumberOfResults++;
                                            }
                                        });
                                    }
                                }
                                catch (err) {
                                    //Loop through the results
                                    $.each(GetAddressesAndIDsResult, function() {
                                        //Check for results
                                        if (this.length > 0) {
                                            //Evaluate JSON
                                            var vAddress = eval("(" + this + ")");

                                            //Create list item
                                            var vOption = $("<option class='AddressOption' value='" + vAddress.uniqueID + "'>" + vAddress.briefDescription + "</option>");

                                            //Find out number of learnt addresses
                                            if (vAddress.uniqueID.indexOf("ConLA") != -1) {
                                                vNumberOfLearntAddresses++;
                                                vLearntAddressUniqueID = vAddress.uniqueID;
                                            }

                                            //Add list item to list box
                                            vListBox.append(vOption);

                                            //Mark result added
                                            vNumberOfResults++;
                                        }
                                    });
                                }

                                //Check if only 1 learnt address was found
                                if (vNumberOfLearntAddresses == 1) {
                                    //Auto select this address
                                    //Set hidden value to the selected items code
                                    $(vHiddenFieldToWriteTo).val(vLearntAddressUniqueID);

                                    //Create postback
                                    $('#ctl00_ContentPlaceHolder1_wizNewConsignment_btnRefresh').click();
                                }

                                //Add list box to div
                                vDivElement.append(vListBox);

                                //Check if any results exist in div
                                if (vNumberOfResults != 0) {
                                    //Append div to page
                                    $("body").append(vDivElement);

                                    //Auto select first item
                                    vListBox.find(".AddressOption:first").attr("selected", "true");
                                }
                            }

                            //Hide loading image
                            vJQElement.css("backgroundImage", "none");
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            //Inform user of error
                            alert("An error occured, please try again");

                            //Hide loading image
                            vJQElement.css("backgroundImage", "none");
                        }
                    });
                }

             }

I have inherited a website! which was designed to work in IE and only IE it seems.. I've now been asked to make the site run in Firefox. I've fixed most of the bugs without any problems but this one has me stumped.

setTimeout(fDelayedFunc, 1000);

This line of Javascript, works fine in IE but in Firefox the function fDelayedFunc never fires. I've removed the setTimeout and the function wrapper and tried running the code as part of the main function. This works without any problems at all.

There is alot of code involved but here's the main but I'm having trouble with. If you'd like to see anymore of the code please let me know.

            setTimeout(fDelayedFunc, 0);

            //Save the current text box value
            var vCurrentTBValue = vJQElement.val();

            function fDelayedFunc() {

                if (vJQElement.val() == vCurrentTBValue) {
                    alert("test");

                    //Get position list box should appear in
                    var vTop = vJQElement.position().top + 25;
                    var vLeft = vJQElement.position().left;

                    //Had to put a special case in for account due to the position co-ords being wrong. This is due to a css error
                    if (vHiddenFieldToWriteTo == "#ctl00_ContentPlaceHolder1_hfAccountCode") {
                        vTop = vJQElement.position().top + 58;
                        vLeft = vJQElement.position().left + 200;
                    }
                    else {
                        vTop = vJQElement.position().top + 25;
                        vLeft = vJQElement.position().left;
                    }


                    //Create div element
                    var vDivElement = $("<div id='divSearchBox' style='position:absolute; top:" + vTop + ";left:" + vLeft + "; z-index: 40000;'></div>");

                    //Create list box
                    var vListBox = $("<select id='lbResults' tabIndex='" + vJQElement.attr("tabIndex") + "' size='4' style='height:400px;'></select>");


                    //Bind a function to the list box which will select the item via either tab or enter
                    vListBox.bind("keydown", function() {
                        //Check if tab or enter has been pressed
                        if (event.keyCode == 9 || event.keyCode == 13) {
                            //Set hidden value to the selected items code
                            $(vHiddenFieldToWriteTo).val($(vListBox.find(":selected")).val());

                            //Create postback
                            $('#ctl00_ContentPlaceHolder1_wizNewConsignment_btnRefresh').click();
                        }
                        //Check if the up arrow has been pressed at the top of the listbox
                        else if (event.keyCode == 38 && $(vListBox.find(":selected")).val() == $(vListBox.find(":first")).val()) {
                            //Focus back on the search box
                            vElement.focus();
                        }
                    }).bind("dblclick", function() {
                        //Set hidden value to the selected items code
                        $(vHiddenFieldToWriteTo).val($(vListBox.find(":selected")).val());

                        //Create postback
                        $('#ctl00_ContentPlaceHolder1_wizNewConsignment_btnRefresh').click();
                    });

                    //Get search field
                    var vSearchText = vJQElement.val();

                    var vDepotID = $("#ctl00_ContentPlaceHolder1_wizNewConsignment_hfDepotID").val();
                    var vCustomerID = $("#ctl00_ContentPlaceHolder1_wizNewConsignment_hfCustomerID").val();
                    var vCountryID = $("#ctl00_ContentPlaceHolder1_wizNewConsignment_hfCountryID").val();
                    var vConsignee = vJQElement.attr("boolConsignee");

                    //Set a loading image in place until call pleted
                    vJQElement.css("backgroundImage", "url(images/small-loader.gif)");
                    vJQElement.css("backgroundRepeat", "no-repeat");
                    vJQElement.css("backgroundPosition", "right");





                    //Make AJAX call
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "NewConsignment.asmx/fGetAddressesAndIDs",
                        data: "{'strSearchText' : '" + vSearchText + "', 'intDepotID' : '" + vDepotID + "', 'intCustomerID' : '" + vCustomerID + "', 'intCountryID' : '" + vCountryID + "', 'boolConsignee' : '" + vConsignee + "'}",
                        dataType: "json",
                        success: function fGetAddressesAndIDsResult(GetAddressesAndIDsResult) {
                            //Make sure there are results
                            if (GetAddressesAndIDsResult != null && GetAddressesAndIDsResult != "") {
                                var vNumberOfResults = 0;
                                var vNumberOfLearntAddresses = 0;
                                var vLearntAddressUniqueID = "";

                                //Try to get results (first call will work on Linux and catch will work on Windows)
                                try {
                                    //Check array exists (if this fails will go to catch)
                                    if (GetAddressesAndIDsResult.d.length > 0) {
                                        //Loop through the results
                                        $.each(GetAddressesAndIDsResult.d, function() {
                                            //Check for results
                                            if (this.length > 0) {
                                                //Evaluate JSON
                                                var vAddress = eval("(" + this + ")");

                                                //Create list item
                                                var vOption = $("<option class='AddressOption' value='" + vAddress.uniqueID + "'>" + vAddress.briefDescription + "</option>");

                                                //Find out number of learnt addresses
                                                if (vAddress.uniqueID.indexOf("ConLA") != -1) {
                                                    vNumberOfLearntAddresses++;
                                                    vLearntAddressUniqueID = vAddress.uniqueID;
                                                }

                                                //Add list item to list box
                                                vListBox.append(vOption);

                                                //Mark result added
                                                vNumberOfResults++;
                                            }
                                        });
                                    }
                                }
                                catch (err) {
                                    //Loop through the results
                                    $.each(GetAddressesAndIDsResult, function() {
                                        //Check for results
                                        if (this.length > 0) {
                                            //Evaluate JSON
                                            var vAddress = eval("(" + this + ")");

                                            //Create list item
                                            var vOption = $("<option class='AddressOption' value='" + vAddress.uniqueID + "'>" + vAddress.briefDescription + "</option>");

                                            //Find out number of learnt addresses
                                            if (vAddress.uniqueID.indexOf("ConLA") != -1) {
                                                vNumberOfLearntAddresses++;
                                                vLearntAddressUniqueID = vAddress.uniqueID;
                                            }

                                            //Add list item to list box
                                            vListBox.append(vOption);

                                            //Mark result added
                                            vNumberOfResults++;
                                        }
                                    });
                                }

                                //Check if only 1 learnt address was found
                                if (vNumberOfLearntAddresses == 1) {
                                    //Auto select this address
                                    //Set hidden value to the selected items code
                                    $(vHiddenFieldToWriteTo).val(vLearntAddressUniqueID);

                                    //Create postback
                                    $('#ctl00_ContentPlaceHolder1_wizNewConsignment_btnRefresh').click();
                                }

                                //Add list box to div
                                vDivElement.append(vListBox);

                                //Check if any results exist in div
                                if (vNumberOfResults != 0) {
                                    //Append div to page
                                    $("body").append(vDivElement);

                                    //Auto select first item
                                    vListBox.find(".AddressOption:first").attr("selected", "true");
                                }
                            }

                            //Hide loading image
                            vJQElement.css("backgroundImage", "none");
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            //Inform user of error
                            alert("An error occured, please try again");

                            //Hide loading image
                            vJQElement.css("backgroundImage", "none");
                        }
                    });
                }

             }
Share Improve this question asked Nov 24, 2010 at 13:24 flyersunflyersun 9173 gold badges16 silver badges35 bronze badges 10
  • 2 Have you put console.log() calls in the function? How do you know it doesn't work? I assure you that setTimeout() definitely does work in Firefox! – Pointy Commented Nov 24, 2010 at 13:32
  • 1 Also, make sure you've installed Firebug before going any further. – Pointy Commented Nov 24, 2010 at 13:33
  • 1 Are you sure the first condition (vJQElement.val() == vCurrentTBValue) is met when the timered function fires? – Marcel Korpel Commented Nov 24, 2010 at 13:38
  • 1 Yes, you already said that; what I meant is: is that condition even true when the timer triggers? Set a breakpoint in Firebug on the first line of that function and check those global variables. And you put that alert within the if statement-block, so it won't show up if the condition isn't met. – Marcel Korpel Commented Nov 24, 2010 at 13:49
  • 1 I had this problem. Seemed like a Firefox bug, had to close the tab and reopen it, then window.setTimeout worked. – user1338062 Commented Sep 30, 2015 at 10:28
 |  Show 5 more ments

3 Answers 3

Reset to default 3

Try this:

setTimeout(function() { fDelayedFunc(); }, 0);

I took setTimeout(function () {function1();}, 1500) out from the addEventListener("load", () => {code})

it worked for me inside Firefox

try this : setTimeout('fDelayedFunc()', 0);

发布评论

评论列表(0)

  1. 暂无评论