如何实现“功能超时”在Javascript中 - 不只是'setTimeout' [英] How to implement a "function timeout" in Javascript - not just the 'setTimeout'

查看:111
本文介绍了如何实现“功能超时”在Javascript中 - 不只是'setTimeout'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Javascript中实现超时,而不是 window.timeout 但类似会话超时套接字超时 - 基本上 - 函数超时

How to implement a timeout in Javascript, not the window.timeout but something like session timeout or socket timeout - basically - a "function timeout"


系统允许经过的指定时间段$ b除非另有指定的
事件发生,否则在指定事件发生之前为$ b;在任何一种情况下,当
任一事件发生时,期限终止。

A specified period of time that will be allowed to elapse in a system before a specified event is to take place, unless another specified event occurs first; in either case, the period is terminated when either event takes place.

具体来说,我想要一个javascript 观察计时器将观察一个函数的执行时间,如果达到或超过指定时间,则观察计时器将停止/通知执行功能。

Specifically, I want a javascript observing timer that will observe the execution time of a function and if reached or going more than a specified time then the observing timer will stop/notify the executing function.

非常感谢任何帮助!非常感谢。

Any help is greatly appreciated! Thanks a lot.

推荐答案

我不完全清楚你在问什么,但我认为Javascript无效你想要的方式是不可能的。例如,无法执行常规函数调用,直到操作完成或持续一定时间(以先到者为准)。这可以在javascript之外实现并通过javascript公开(就像使用同步ajax调用一样),但不能在具有常规函数的纯javascript中完成。

I'm not entirely clear what you're asking, but I think that Javascript does not work the way you want so it cannot be done. For example, it cannot be done that a regular function call lasts either until the operation completes or a certain amount of time whichever comes first. That can be implemented outside of javascript and exposed through javascript (as is done with synchronous ajax calls), but can't be done in pure javascript with regular functions.

不同其他语言,Javascript是单线程的,所以当一个函数执行时,一个计时器将永远不会执行(除了Web工作者,但他们可以做的非常非常有限)。定时器只能在函数完成执行时执行。因此,您甚至无法在同步函数和计时器之间共享进度变量,因此计时器无法检查函数的进度。

Unlike other languages, Javascript is single threaded so that while a function is executing a timer will never execute (except for web workers, but they are very, very limited in what they can do). The timer can only execute when the function finishes executing. Thus, you can't even share a progress variable between a synchronous function and a timer so there's no way for a timer to "check on" the progress of a function.

如果你的代码是完全独立的(没有访问你的任何全局变量,没有调用你的其他函数并且无论如何也没有访问过DOM),那么你可以在web-worker中运行它(可用)仅在较新的浏览器中)并在主线程中使用计时器。当web-worker代码完成时,它会向主线程发送一条消息及其结果。当主线程收到该消息时,它会停止计时器。如果计时器在接收结果之前触发,则可以终止Web工作者。但是,您的代码必须遵守Web工作者的限制。

If your code was completely stand-alone (didn't access any of your global variables, didn't call your other functions and didn't access the DOM in anyway), then you could run it in a web-worker (available in newer browsers only) and use a timer in the main thread. When the web-worker code completes, it sends a message to the main thread with it's results. When the main thread receives that message, it stops the timer. If the timer fires before receiving the results, it can kill the web-worker. But, your code would have to live with the restrictions of web-workers.

Soemthing也可以通过异步操作完成(因为它们可以更好地使用Javascript的单线程 - ness)像这样:

Soemthing can also be done with asynchronous operations (because they work better with Javascript's single-threaded-ness) like this:


  1. 启动异步操作,如ajax调用或加载图像。

  2. 使用 setTimeout()启动计时器以获取超时时间。

  3. 如果计时器在异步操作完成之前触发,则停止异步操作(使用API​​取消它)。

  4. 如果异步操作在计时器触发之前完成,则用 clearTimeout()取消计时器并继续。

  1. Start an asynchronous operation like an ajax call or the loading of an image.
  2. Start a timer using setTimeout() for your timeout time.
  3. If the timer fires before your asynchronous operation completes, then stop the asynchronous operation (using the APIs to cancel it).
  4. If the asynchronous operation completes before the timer fires, then cancel the timer with clearTimeout() and proceed.

例如,以下是如何在图像加载时设置超时:

For example, here's how to put a timeout on the loading of an image:

function loadImage(url, maxTime, data, fnSuccess, fnFail) {
    var img = new Image();

    var timer = setTimeout(function() {
        timer = null;
        fnFail(data, url);
    }, maxTime);

    img.onLoad = function() {
        if (timer) {
            clearTimeout(timer);
            fnSuccess(data, img);
        }
    }

    img.onAbort = img.onError = function() {
        clearTimeout(timer);
        fnFail(data, url);
    }
    img.src = url;
}

这篇关于如何实现“功能超时”在Javascript中 - 不只是'setTimeout'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆