【问题标题】:Javascript block multiple executionsJavascript 阻止多次执行
【发布时间】:2019-06-04 23:20:09
【问题描述】:

我目前正在使用 Web 框架编写 Python 应用程序。我实现了一个调用 javascript 函数,调用 Python WebService 的表单。这些网络服务非常繁重,所以我想实现一个安全性以避免多次运行 javascript 并在允许另一个调用之前等待 WS 响应。

这是一段 HTML:

<div class="form-group">
    <input onfocusout="ocr_on_fly(false, this)" onfocusin="ocr_on_fly(true, this)" type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
</div>

ocr_on_fly函数中JS中WS的调用:

onSelectEnd: function(img, selection){
    if(selection['width'] !== 0 && selection['height'] !== 0){
        $.post({
        url: "http://localhost:5000/pdf/ocr",
        data: {
            data: JSON.stringify({
                selection   : selection,
                fileName    : $('#my-image')[0].src.replace(/^.*[\\\/]/, ''),
                thumbSize   : {width: img.width, height:img.height}
            })
        }

        }).then(function(data) {
                input.value = data['result'];
        });
    }
}

我听说过 setTimeout,但不确定它是否更好,因为我不想在指定时间内阻止脚本的执行。该脚本可能需要 0.1 秒,因为它可能需要 10 秒或更长时间

提前致谢

【问题讨论】:

  • 我认为这应该在python,webservice-side中完成。 Javascript 应该只接收Please wait for a previous process to end 消息。
  • 你希望发生什么 - 如果调用在短时间内执行了 3 次,你应该得到第一个结果还是只得到最后一个结果?
  • 您可以轻松设置一个全局变量,当您调用脚本时将其设置为true,当响应为false时,如果值为true,则webservice不执行
  • 有多种方法可以限制来自客户端的请求,包括为您处理此问题的库,或者简单地检查请求是否在进行中。

标签: javascript web-services


【解决方案1】:

您可以设置一个全局变量,然后在调用脚本时将其设置为true

然后,当响应完成后,您将其设置为false

在发出请求之前检查这个变量;如果它已经设置为true,请不要提出请求。

【讨论】:

    猜你喜欢
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 2010-09-30
    • 1970-01-01
    • 2018-08-27
    • 2012-04-18
    • 2017-05-07
    • 2021-01-11
    相关资源
    最近更新 更多