FrameDev
FrameDev

Reputation: 61

Pause all javascript

I have been working on writing a library of code for my future projects. One of the functions I've been working on is a pause function. So far I have no problem with the errors reporting that the script is running to long even on pauses as long as 10 seconds. This is to primarily keep malicious users busy, it works well when you set a very long time. I was wondering if there are any errors that I should look out for that I might face? Here's the code...

pause = function(a) {
    var b = new Date().getTime();
    e = false;
    function wait() {
    d=10;
    for(i=0;i<d;i++) {
        d++;
        var c = new Date().getTime();
        if(c-b>=a) {
            e = true;
            break;
        }
        if(d>1000000) {
            break;
        }
    }
}
wait();
if(e==false) {
    pause(a-(new Date().getTime()-b));
}};

Upvotes: 0

Views: 3055

Answers (3)

jfriend00
jfriend00

Reputation: 707446

As I've said in my comments so far, trying to run semi-infinite loops in javascript is never a good idea.

Here are some of the issues to watch out for:

  1. If you don't return to the browser event loop by finishing your javascript thread of execution after some period of time, the browser will stop and prompt the user to kill the javascript in that browser window.
  2. Your code uses a bunch of undeclared variables which makes them implicit global variables. Since they have common names, they could easily interfere with other code.
  3. Because javascript is single threaded, while your loop is running, no other browser events for that window can run rendering the browser window essentially frozen. Users will think it is hung. Patient users might wait longer to see if it completes. Impatient users will kill the browser window.

Essentially what you're trying to do is a denial-of-service attack on a user's browser. You may not like what they're doing to your web page, but attacking them back does not seem appropriate. Much better to just block access to your page in a browser-appropriate way rather than a browser-attacking way.

Suggestions:

If what you're really trying to do is just render your page unuseful in some circumstances (you still haven't explained why you're trying to do this or what condition triggers your desire to do this), then it might be better to just make the page unuseful or informative without trying to kill the browser. You could do that by:

  1. Just hiding everything in the DOM so the window goes blank.
  2. Remove all DOM elements so the window goes blank.
  3. Put a transparent layer over the whole window so that no input events can get to the page elements, but the visuals stay there. You could add a message to that window.
  4. Replace the contents of your page with an appropriate message.

Upvotes: 1

Chris Perkins
Chris Perkins

Reputation: 809

Javascript already has a way to do that. Use setTimeout() and pass it an anonymous function with the continuation of your operation. The second argument is the delay in milliseconds.

Example:

setTimeout(function(){ alert('hello'); }, 2000);

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 993243

You never ever want to do this sort of thing in Javascript. As you have noticed, the browser will complain about your script taking too long. Furthermore, this will take much more energy than necessary which is important for mobile devices with limited battery capacity.

Instead, use the standard setTimeout() function to run code at a later time.

Upvotes: 4

Related Questions