Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

javascript - 如何在Javascript中实现“函数超时”-不仅仅是“ setTimeout”(How to implement a “function timeout” in Javascript - not just the 'setTimeout')

How to implement a timeout in Javascript, not the window.timeout but something like session timeout or socket timeout - basically - a " function timeout "(如何在Javascript中实现超时 ,而不是window.timeout而是session timeoutsocket timeout类的东西-基本上-一个“ function timeout ”)

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.(在任何一种情况下,该事件在发生时都将终止。)

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.(具体来说,我想要一个JavaScript observing timer ,该observing timer将观察功能的执行时间,如果达到或超过指定时间, observing timer将停止/通知执行功能。)

Any help is greatly appreciated!(任何帮助是极大的赞赏!)

Thanks a lot.(非常感谢。)   ask by sransara translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.(我不清楚您要问的是什么,但我认为Javascript无法按照您想要的方式工作,因此无法完成。)

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之外实现并通过javascript公开(与同步ajax调用一样),但是不能在具有常规功能的纯javascript中实现。)

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).(与其他语言不同,Javascript是单线程的,因此在执行函数时,计时器将永远不会执行(Web工作者除外,但他们的工作能力非常有限)。)

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.(因此,您甚至无法在同步功能和计时器之间共享进度变量,因此计时器无法“检查”功能的进度。)

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.(如果您的代码是完全独立的(无法访问任何全局变量,没有调用您的其他函数并且也没有访问DOM),那么您可以在网络工作者中运行它(可在仅限较新的浏览器),并在主线程中使用计时器。)

When the web-worker code completes, it sends a message to the main thread with it's results.(Web工作者代码完成后,它将一条消息及其结果发送到主线程。) 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 can also be done with asynchronous operations (because they work better with Javascript's single-threaded-ness) like this:(Soemthing也可以通过异步操作完成(因为它们可以更好地与Javascript的单线程性一起工作),如下所示:)

  1. Start an asynchronous operation like an ajax call or the loading of an image.(启动异步操作,例如ajax调用或图像加载。)
  2. Start a timer using setTimeout() for your timeout time.(使用setTimeout()作为您的超时时间来启动计时器。)
  3. If the timer fires before your asynchronous operation completes, then stop the asynchronous operation (using the APIs to cancel it).(如果计时器在异步操作完成之前触发,请停止异步操作(使用API??取消它)。)
  4. If the asynchronous operation completes before the timer fires, then cancel the timer with clearTimeout() and proceed.(如果异步操作在计时器触发之前完成,请使用clearTimeout()取消计时器并继续。)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...