简体   繁体   English

上一个功能完成后执行javascript函数

[英]execute javascript function when previous function has completed

This has been bothering me all day, if anyone can help I would be greatful. 这整天困扰着我,如果有人可以帮助我,我将非常感激。

if(aaid) {
    var num_one = aaid;
    var radioBox = document.getElementsByName('used_dealer_before');
    for (i = 0; i < radioBox.length; i++) {
        if(num_one) {
            radioBox[i].checked = true; //this auto checks the box
        }
    }
    radioBox.onchange=function(){
        document.getElementById("previous_dealer").value=num_one; //this auto selects the     value from the dropdown
    }
}

The function above auto checks a radiobox if a variable (aaid) is present, once this box is checked there is another function in another script file (to which I do not have access; long story) that presents a dropdown menu. 上面的功能会自动检查单选框是否存在变量(aaid),一旦选中此框,另一个脚本文件(我无权访问;长话短说)中就会出现另一个功能,该功能会显示一个下拉菜单。 The next part of MY script auto-selects a value on the dropdown. MY脚本的下一部分将自动在下拉列表中选择一个值。 This script works and has been tested locally. 该脚本有效,并已在本地测试。 However, when I upload it to the server it does not execute the 2nd part of the script anymore. 但是,当我将其上传到服务器时,它不再执行脚本的第二部分。 I suspect because the function already executes, when the page hasn't finished rendering the dropdown value. 我怀疑是因为页面尚未完成渲染下拉值时该函数已经执行。 Therefore, can anyone explain to me how to use JavaScript to wait for the dropdown to appear, before it continues with the script? 因此,谁能向我解释在继续脚本之前如何使用JavaScript等待下拉列表出现?

Should I time delay the function? 我应该延迟功能吗? I cannot use jQuery, I have to use JavaScript. 我不能使用jQuery,必须使用JavaScript。

It sounds like you're looking to set up a callback function. 听起来您正在寻找设置回调函数。 This is commonly done when requesting external services using XHR/AJAX to get external data or code. 使用XHR / AJAX请求外部服务以获取外部数据或代码时,通常会这样做。 For instance, when you want something from a server and want to be notified when it's ready, you might define a callback function using a query string parameter: 例如,当您要从服务器获取某些信息并希望在其就绪时收到通知时,可以使用查询字符串参数定义一个回调函数:

http://somedomain.com/resources?arg1=val1&callback=scriptready

The value given for the callback parameter would correspond to some globally accessible function: 为callback参数提供的值将对应于一些可全局访问的函数:

window['scriptready'] = function(val){ console.log("got response: "+val); }

If this resource you're using provides the option of using a callback, then that's you're answer. 如果您正在使用的该资源提供了使用回调的选项,那么就是您的答案。 If it doesn't, then you'll need some more convoluted method for setting up a listener based on when your box is checked or whatever. 如果没有,那么您将需要一些更复杂的方法来基于何时选中复选框或其他方式来设置侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM