简体   繁体   English

Java脚本函数保存,直到保存evenet执行完成

[英]Java script function to hold until save evenet execution is completed

I have a javascript function. 我有一个javascript函数。 In thi function i am calling a button event like below. 在这个功能中,我正在调用下面的按钮事件。

$("#btnSave").trigger("click");

My query is , Is there any way to keep the control here on this line until the saving is done? 我的查询是,在保存完成之前,有没有办法将控件放在此行上? I have some code written underneath this line and it is being overridden. 我在这一行下面写了一些代码,它被覆盖了。

Any suggestions? 有什么建议么?

Use a callback. 使用回调。

Get the lines under the trigger line and pass to your save function as callback when the event has success. 获取触发行下的行,并在事件成功时作为回调传递给保存函数。

It would have helped if you've posted some code. 如果您发布了一些代码,它会有所帮助。

You can do it in 2 ways: 你可以用两种方式做到:

1.) Use polling. 1.)使用轮询。 After the trigger call use a loop to check for a flag that you must set when the save is complete. 触发器调用后,使用循环检查保存完成时必须设置的标志。 A timeout is needed for saving CPU from intensive js processing. 从强化js处理中节省CPU需要超时。

2.) Put the code to be executed after the trigger call inside a function. 2.)将触发调用后执行的代码放入函数内。 Pass this function as a callback to the onClick function. 将此函数作为onClick函数的回调函数传递。

    function onSaveClick(e) {
        //do my save stuff

        e.data.callback();
    }

No. 2 is recommended. 推荐2号。 //attach onclick event $("#btnSave").click(onSaveClick); //附上onclick事件$(“#btnSave”)。click(onSaveClick);

//you onclick function
function onSaveClick(event, callback) {
    //save data
    callback();
}

//trigger
$("#btnSave").trigger("click", afterSave);

//after save stuff
function afterSave(){
//do more stuff
}

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

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