简体   繁体   English

如何在执行另一个功能之前等待一个功能完成

[英]How to wait for a function to complete before executing another function

I'm using selecter jquery . 我正在使用选择器jquery I initialize it by typing the code 我通过输入代码对其进行初始化

$("select").selecter();

I need to make sure that the formstone selecter jquery library has completed before i start appending elements. 我需要确保在开始添加元素之前,formstone选择器jquery库已完成。 So what i did is to is use the $.when function 所以我要做的是使用$.when函数

initialize: function(){
    $.when($("select").selecter()).then(this.initOptions());
},

initOptions: function(){
    this.$el.find('.selecter').addClass('something');
}

But this does not work. 但这是行不通的。 How can i wait while formstone selecter is doing its thing before i execute another function? 在执行其他功能之前,我如何等待formstone selecter做事?

Thanks, 谢谢,

UPDATE 更新

Here's the update of what i did but it does not work. 这是我所做的更新,但不起作用。

initialize: function(){
   $("select").selecter({callback: this.initOptions });
},

initOptions: function(){
    this.$el.find('.selecter').addClass('something');
}

There is a callback option. 有一个回调选项。

The function passed as a callback will receive the newly selected value as the first parameter 作为回调传递的函数将接收新选择的值作为第一个参数

Should be $("select").selecter(callback: function() { alert('callback fired.') }); 应该是$("select").selecter(callback: function() { alert('callback fired.') }); or as shown 或如图所示

$("select").selecter({
    callback: selectCallback
});

function selectCallback(value, index) {
    alert("VALUE: " + value + ", INDEX: " + index);
}

The problem which I think regarding the callback edited code is that this can refer to anything. 我认为有关回调编辑代码的问题是,它可以引用任何内容。 Try the following code 试试下面的代码

var selectorObj = {
 initialize: function(){
    $("select").selecter({callback: selectorObj.initOptions });
    },
initOptions: function(){
    this.$el.find('.selecter').addClass('something');
     }
};

Created a working fiddler for you http://jsfiddle.net/6Bj6j/ 为您创建了一个工作的提琴手http://jsfiddle.net/6Bj6j/

The css is out of shape. CSS变形。 Just select what is poping up when you click on the dropdown. 只需选择单击下拉菜单时弹出的窗口。 You will get an alert which is written in the callback. 您将收到一个写在回调中的警报。

The problem with the provided snippet is the scope of the callback: 提供的代码段的问题在于回调的范围:

var selectorObj = {
    initialize: function(){
        $("select").selecter({ callback: selectorObj.initOptions });
    },
    initOptions: function(){
        // 'this' refers to the "$('.selecter')" jQuery element
        this.addClass('something');
    }
};

However if you just need to add a class to the rendered element, you should use the 'customClass' option: 但是,如果您只需要向渲染的元素添加类,则应使用“ customClass”选项:

$("select").selecter({
    customClass: "something"
});

If you need to do more, you can always access the Selecter element directly: 如果需要执行更多操作,则始终可以直接访问Selecter元素:

var $selecter = $("select").selecter().next(".selecter");

$selecter.addClass("something").find(".selecter-selected").trigger("click");

Sidenote: I'm the main developer of Formstone. 旁注:我是Formstone的主要开发人员。 If you have any suggestions for new features or better implementation, just open a new issue on GitHub. 如果您对新功能或更好的实现有任何建议,只需在GitHub上打开一个新期刊。

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

相关问题 等待功能完成,然后再执行下一个命令 - Wait for function to complete before executing next command 如何在运行另一个 function 之前等待 fetch 完成 - How to wait for fetch to complete before running another function 在触发另一个之前等待获取 function 完成 - Wait for fetch function to complete before firing another 在componentWillMount中执行另一个函数之前,如何等待函数完成? - How to wait for function to finish before executing another in componentWillMount? 在继续之前等待功能完成 - Wait for function to complete before moving on 在执行另一个函数之前,如何使jquery等待一个函数完成? - How can I make jquery wait for one function to finish before executing another function? 在另一个异步 function 中执行代码之前,如何在异步 function 中等待循环完成? - How to wait for a loop to finish in an async function before executing code in another async function? 如何在运行第二个 function javascript 之前等待第一个 function 完成 - How to wait for first function to complete before running second function javascript 如何在执行下一个 function 之前等待 function 一定延迟 - How to wait for a function for a certain delay before executing the next function 如何在执行另一个功能之前等待某个功能完成? - How do I wait for a certain function to finish before executing another one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM