Refresh the screen after 10 seconds


Apologies if I'm not explaining this well in advance.
I am looking to create a dynamic webpage using JavaScript that automatically updates every 10 seconds. Does anyone have an example code snippet for this?

**Specifically, allowing the user to manually refresh the page every 10 seconds**
I want my website visitors to have the option to refresh the page every 10 seconds while browsing.

Answer №1

A customized button can be created to appear after 10 seconds, giving the user the option to update their information without being forced to do so.

setTimeout(function(){
   let button = document.querySelector('button');
   button.style.display = "block";
   button.onclick = () => window.location.reload();
}, 10000);

window.onkeydown = (e) => {
  if(e.key === 'F5'){
    e.preventDefault()
  }
}
button {
display:none;
}
<button>Click me</button>

Answer №2

UPDATE: While it's not possible to completely prevent a user from reloading a page, you can prompt them if they really intend to leave using the script below. After 10 seconds of no interaction, it won't prompt anymore. But within that timeframe, it will ask for confirmation before leaving.

var confirm = 0;

window.onbeforeunload = function() {
    if(confirm == 1) {
        return "Are you sure you want to leave?";
    }
}

setTimeout(function(){
    confirm = 0;
}, 10000);

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

"Obtain a DOM element within an Angular directive by using the jQuery find method

When inspecting the DOM, I can see the element anchor tag present but cannot retrieve it using jquery.find(). The console returns a length of 0, preventing me from initializing angular xeditable on that element. angular.module('built.objects') ...

calculating the size of an array in node.js

I received a form object from my AngularJS to Node.js and this is how it looks in the console: For two files, filenames : [object Object],[object Object] # filenames object for two files length: 2 # used object.length For one file, filenames : [object ...

Using VueJS to switch classes on multiple cards

There is a page with multiple cards, each containing its own set of status radio buttons: ok, missing, error. The goal is to be able to change the status of individual cards without affecting others. A method was created to update the class on the @change ...

Guide on making a personalized object in JavaScript

I am struggling with a piece of JavaScript code that looks like this: var myData=[]; $.getJSON( path_url , function(data){ var len = data.rows.length; for (var i = 0; i < len; i++){ var code = data.rows[i].codeid; var ...

Choosing multiple lists in Angular 2 can be achieved through a simple process

I am looking to create a functionality where, upon clicking on multiple lists, the color changes from grey to pink. Clicking again will revert the color back to grey. How can I achieve this using class binding? Below is the code snippet I have tried with ...

Using JavaScript to transform JSON information into Excel format

I have tried various solutions to my problem, but none seem to fit my specific requirement. Let me walk you through what I have attempted. function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) { //If JSONData is not an object then JSON.parse will ...

Failure to install Playwright browsers in the continuous integration environment following the upgrade to a more recent version

, we are currently facing the following situation: For our e2e Playwright tests that rely on JS/TS, two crucial steps in our CI pipeline include: - Running the script: npm ci Displayed as 'npm ci' - Running the script: sudo npx playwright ins ...

Using the Firebase Admin SDK for user creation on the server side

**While working on my CRUD project, I encountered an issue where the admin needed to add a user in the documentation as well as in the authentication process. Using the normal SDK would simply replace the current user, so I attempted to use the admin SDK. ...

Warning: The minimatch package is no longer being actively supported

When attempting to run npm install, I encountered the following error: npm WARN deprecated [email protected]: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue The node version I am using is v4.1.1 Any suggestions on how ...

Retrieve the nearest attribute from a radio button when using JQuery on a click event

I have a query regarding the usage of JS / JQuery to extract the data-product-name from a selected radio button, prior to any form submission. Although my attempt at code implementation seems incorrect: return jQuery({{Click Element}}).closest("form" ...

I'm experiencing some issues with AwaitingReactions in Discord.js, it's not working properly on

I'm having trouble with implementing reaction awaiting as per the guide in discord.js. For some reason, it just doesn't seem to work on my end. No matter what I try, when I click the emoji, it doesn't register. I've scoured numerous Sta ...

displaying only the date in bootstrap-datetimepicker

I am currently utilizing the repository created by smalot, and my intention is to choose and display dates only (while in other instances I showcase both date and time, hence utilizing this repository). Although I have succeeded in selecting dates only, th ...

Capture - retrieve the data of the specified route using a specific keyword

Query I am managing multiple nested routers and I need to retrieve the entire string that corresponds to the request's path. To clarify, please refer to this code snippet: const express = require('express') const app = express() const rout ...

Utilizing JavaScript to retrieve data using AJAX

Having trouble sending emails through ajax on my website. The form is in a modal (using custombox), but all values being sent are null. How can I utilize getElementById in modal content? Link to custombox git's hub: https://github.com/dixso/custombox ...

Efficient Error Handling in Next.JS with Apollo GraphQL Client

Although the component successfully renders the error state, an uncaught exception is displayed in the console and a dialogue box appears in the browser. How can expected errors be handled to prevent this behavior? import { useMutation, gql } from "@a ...

Position of JSON data response is inaccurate

Currently, I have a function that calls an API from the server in the following manner: getDataSet(callback) { request.open('POST', `${apiPath}`); request.setRequestHeader('Content-Type', 'application/x-ww ...

Storing input field values in JavaScript using the onchange event handler.Would you like a different revision

I am looking to calculate the area by multiplying width and height entered into input fields, and then display the result. Any guidance would be greatly appreciated. Thank you! Here is my current code. const width = document.querySelector("#width"); con ...

When setupFilesAfterEnv is added, mock functions may not function properly in .test files

Upon including setupFilesAfterEnv in the jest.config.js like this: module.exports = { preset: 'ts-jest', testEnvironment: 'node', setupFilesAfterEnv: ["./test/setupAfterEnv.ts"] } The mock functions seem to sto ...

Acquiring images from an external source and storing them in either the $lib directory or the static folder during the

Currently, I have a Sveltekit project set up with adapter-static. The content for my pages is being pulled from Directus, and my images are hosted in S3, connected to Directus for easy access. I am also managing audio samples in a similar manner. During b ...

Retrieving Ajax Data Using C#

I am struggling to send data through Ajax to a C# file. Whenever I check the received data, it always comes back as null. Could there be an issue in my code? Javascript file $(".save").click(function () { var ss = "Helloo!"; $.ajax({ typ ...