是否可以在等待事件监听器时停止执行某个函数?

时间:2021-03-01 20:37:48

Using the gmail.js library I wrote this function that receives a string back from an event listener that I want to inject into the body of my email before it is sent. How can I somehow get the function to halt execution while waiting for the Event listener to get the reply from the external script? I have a feeling it can be done with synchronous callbacks but I dont know those well enough to know how to. I'd appreciate any help on the matter.

使用gmail.js库我编写了这个函数,它从一个事件监听器接收一个字符串,我希望在发送之前将其注入我的电子邮件正文。在等待事件监听器从外部脚本获取回复时,我怎么能以某种方式让函数停止执行?我有一种感觉,它可以通过同步回调完成,但我不知道那些足够好知道如何。我对此事有任何帮助表示感谢。

gmail.observe.before('send_message', function(url, body, data, xhr) {

console.log('In the before send message method');

var event = new CustomEvent('Marketing', {

    detail: {
        body: xhr.xhrParams.body_params.body,
        email: gmail.get.user_email()
    }
});

window.addEventListener('message', function(event) {
if (event.data.type && (event.data.type == "FROM_PAGE")) {
    console.log("received a reply");
    console.log(event.data.text);
    newstring = (event.data.candidate);
    console.log(newstring);
    console.log(event.data.token);
    gotNewString = true;
}
});


window.dispatchEvent(event);
console.log('sent the message to extension.js');
function listen(data){
    console.log("123123",JSON.stringify(data));
    console.log(data);
}

newstring = "Why must you torture me so?";

var oldCmml = xhr.xhrParams.url.cmml;

var body_params = xhr.xhrParams.body_params;

if (newstring.length > oldCmml) {
    xhr.xhrParams.url.cmml = newstring.length;
} else {
    while (newstring.length < oldCmml) {
        newstring += ' ';
    }
    xhr.xhrParams.url.cmml = newstring.length;
}
console.log(newstring);
body_params.body = newstring;

console.log("sent");

});

1 个解决方案

#1


0  

You can't stop the execution of a function inside a callback. You would have to change the code of the calling function, to wait for your function to be finished. This is normally done by supplying a callback function, that you than have to call, once you are done processing, for instance:

您无法在回调中停止执行函数。你必须改变调用函数的代码,等待你的函数完成。这通常通过提供一个回调函数来完成,您必须在完成处理后进行调用,例如:

gmail.observe.before('send_message', function(url, body, data, xhr, done) {
    // you can do processing here

    // and have to call done, to let the caller continue his work
    done();
});

For listeners, this is obviously normally not supplied, but since gmail.js is open-source you could change it?

对于听众来说,这显然通常不会提供,但由于gmail.js是开源的,你可以改变吗?

#1


0  

You can't stop the execution of a function inside a callback. You would have to change the code of the calling function, to wait for your function to be finished. This is normally done by supplying a callback function, that you than have to call, once you are done processing, for instance:

您无法在回调中停止执行函数。你必须改变调用函数的代码,等待你的函数完成。这通常通过提供一个回调函数来完成,您必须在完成处理后进行调用,例如:

gmail.observe.before('send_message', function(url, body, data, xhr, done) {
    // you can do processing here

    // and have to call done, to let the caller continue his work
    done();
});

For listeners, this is obviously normally not supplied, but since gmail.js is open-source you could change it?

对于听众来说,这显然通常不会提供,但由于gmail.js是开源的,你可以改变吗?