Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent screen sleep using javascript

Tags:

javascript

I have a use case where we donot want the user who is taking a test get sleep timeout while in test which is of 3 hrs. I was looking at nosleep.js but i has cpu overuse problem as it was not working in windows.

I was wondering how test taking apps do it, also how youtube achieves this.

Any help on this is really appreciated

like image 425
INFOSYS Avatar asked Dec 24 '19 16:12

INFOSYS


People also ask

How do I keep my screen awake in Javascript?

style. background = 'black'", 100000); This code will set the background of the document to black, which will keep the screen awake.

How do you keep your screen awake?

Go to Control Panel > Personalization > Change Screensaver. Next to On Resume, Display Logon Screen, uncheck the box. This prevents your system from sleeping.


2 Answers

Wake Lock API arrives in Chrome 79. However none of the other browser support this as of now.

https://developers.google.com/web/updates/2019/12/nic79#wake-lock

like image 176
Useful Angle Avatar answered Sep 28 '22 09:09

Useful Angle


YouTube handles this by simply playing video. When you play a video with sound in the browser, the browser requests a wake lock automatically so as not to interrupt the user.

It isn't clear from your question what your specific requirements are, but if playing a video is possible, try that.

like image 31
Brad Avatar answered Sep 28 '22 10:09

Brad