Ways to determine if web pages are still not fully loaded after 12 seconds using JavaScript

Looking to optimize my code, I am only interested in targeting devices that have a slower page loading time, perhaps those taking more than 12 seconds to fully load.

Answer №1

document.readyState is a useful property that returns "complete" once the page has finished loading. To ensure that your page doesn't stay hanging, you can set a timeout and redirect to another URL if the value of readyState is not "complete" after 12 seconds.

setTimeout(function () {
    // The document may not be fully loaded yet.
    if (document.readyState !== 'complete') {
        top.location.href = 'http://myweb.com/super-minimal-page.html';
    }
}, 12000);

Answer №2

Create a Boolean variable at the beginning of your script and initialize it with a value of false. Then, set up a setTimeout function for 12 seconds that will call a specific function in case the page is not loaded.

Inside the document.load function, change the Boolean variable to true and then check for its value in your pageNotLoaded function as shown below:

<script type="text/javascript>
var pageIsLoaded = false;
setTimeout(PageNotLoaded, 12000);

$(function(){

pageIsLoaded = true;

});

function PageNotLoaded()
{
if(!pageIsLoaded)
{
// Insert your code here
}
}
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are the steps to integrate jQuery into an Angular 8 application?

I am currently working on an application that relies on SignalR for communication with a desktop application. In order to make use of SignalR, I include jQuery in my .ts file. However, after migrating from Angular 7 to Angular 8, it appears that this setup ...

The Bootstrap navigation bar is experiencing functionality issues when viewed on Chrome mobile browsers

I'm currently testing out the bootstrap 3 navbar feature on my meteor application, but I'm encountering some issues specifically on mobile devices. The problem seems to be that the toggle button is not showing up as expected. Interestingly, it wo ...

Executing a keystroke in Selenium Webdriver using JavaScript

I've been writing a test using Selenium WebDriverJS, and now I need to simulate pressing a key on the keyboard. Is it possible to do this with Selenium WebDriverJS? If so, how can it be done? In Java, we achieve this as follows: driver.findElement(Lo ...

Calculating the quantity of choice elements within a dropdown menu using jQuery

Is there a way to calculate the number of <option> elements in a <select> element using jQuery? <select data-attr="dropdown" id="input1"> <option value="Male" id="Male">Male</option> <option value="Female" id="Female"& ...

Strategies for transferring a dynamic variable to a parent window's form in JavaScript?

What is the best way to use a changing parameter name when accessing a variable? For instance: opener.document.form.namerow_3.value = this.cells[0].innerHTML; opener.window.form.{varaible}.value=this.cells[0].innerHTML; In this scenario, the parameter ...

Using jQuery to implement AJAX file uploads

Currently, I am facing a challenge. I have multiple forms on a single page that are being submitted to the backend asynchronously via ajax. Some of these forms require a file upload without interrupting the process, so it needs to be handled asynchronousl ...

Utilizing a loop for setting variable values in JavaScript

Using both JavaScript and JQuery. Let's imagine there is an array called ListArray with various sentences inside. Sounds easy enough. Is there a way to achieve this? var List = for (var i = 0; i < 10; i++) { //iterate over an array here to c ...

Modify the class of an input while typing using Jquery

Recently, I created a form using Bootstrap 4. The validation process is done through a PHP file with an AJAX call and it's functioning correctly, except for one issue. I want the input class to switch from "invalid" to "valid" as soon as the user begi ...

The functionality of a basic each/while loop in jQuery using CoffeeScript is not producing the desired results

I've been experimenting with different methods to tackle this issue. Essentially, I need to update the content of multiple dropdowns in the same way. I wanted to use an each or a while loop to keep the code DRY, but my experience in coffeeScript is li ...

exploring the realm of creating constants in AngularJS functions

controller.js angular.module('app.main') .controller('MainCtrl', function ($scope, currentUser, addAPI) { $scope.form = {}; $scope.subdomain = currentUser.domainName; $scope.add = function () { addAPI.addAdmin(loc ...

In Vue.js, modifying a parent component variable using $parent does not update the interpolation syntax

Child component <template> <div> <h3>Child Component</h3> <div> <button @click="changeValue()">Update Parent Value</button> </div> </div> </template> <script> export ...

Retrieving data from several checkboxes in AngularJS

Here is my code snippet where I am attempting to retrieve values of multiple selected checkboxes, but it seems to be malfunctioning. Can you please guide me on what mistake I might be making? Just to clarify, the list is dynamic and will not be limited to ...

What is the best way to access the data from a CSV file that has been uploaded using ngFile

Is there a way to use ngFileUpload to read the contents of a csv file before saving it to a database? $scope.checkAndUploadFile = function (file) { $scope.uploadFileProgress = true; Upload.upload({ method: 'POST', url: & ...

Error: The parent selector "&" cannot be used in top-level selectors. $::webkit-input-placeholder

I am facing an issue while trying to run a legacy create-react-app that utilizes Sass. Initially, when I ran npm start, I encountered the error 'Cannot find module sass', which resembled the message found in this stack overflow post. To resolve t ...

Floating elements changing positions as the user scrolls

Have you ever wondered how websites like Facebook manage to float certain elements, such as a menu bar or social plugin, when the user scrolls past a specific point? This functionality can be achieved through JavaScript (jQuery) events that trigger when a ...

A guide on incorporating onclick() functions into pdfjs

Currently, I'm attempting to implement an onclick() event in pdfjs. After attempting to add a method within the pdf.js file to handle the onclick functionality, I encountered an error upon clicking "Uncaught ReferenceError: displayAttachmentsOnLocati ...

Subscribe on Footer triggers automatic scrolling to the bottom of the page

Whenever I fill out the form in the footer and hit submit, it directs me to a different page while automatically scrolling back down to the footer. I'm looking for a solution that prevents this automatic scrolling. I've attempted using window.sc ...

Connect the AngularJS data model with a Foundation checkbox element

I am attempting to link a model (a boolean value) to a checkbox created with Foundation (Zurb). I have created a small demonstration displaying the issue: http://jsfiddle.net/JmZes/53/ One approach could involve simply creating a function that triggers o ...

Deactivate the submit button without applying a grayed-out appearance

Is it possible to prevent an HTML form submit button from being greyed out when disabled? Issue at hand: I am looking to disable the entire form, but find that having the submit button grey out before the rest of the form does not look visually appealing. ...

Div element with fixed position that dynamically adjusts its height according to the position of the

Currently, I am designing a website with two sidebars and a center wrapper for the main content. The sidebars will contain links that jump the user to different sections of the page. I have fixed the position of the sidebars so they stay visible as the use ...