Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8711373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:51:40+00:00 2026-06-13T04:51:40+00:00

I have code background js: chrome.contextMenus.onClicked.addListener(function(tab) { chrome.tabs.executeScript(null, { code: setInterval(function(){alert(‘test’)}, 2000) }); });

  • 0

I have code background js:

chrome.contextMenus.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(null, {
        code: "setInterval(function(){alert('test')}, 2000)"
    });
});

var id = chrome.contextMenus.create({
    "title": "Auto Refresh",
    "contexts": ["page"]
});​

It works, each 2 seconds it have a alert box “test”;

But I replace alert(‘test’) by location.reload:

chrome.contextMenus.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(null, {
        code: "setInterval(function(){location.reload()}, 2000)"
    });
});

var id = chrome.contextMenus.create({
    "title": "Auto Refresh",
    "contexts": ["page"]
});​

It only have 1 refresh for the first time and don’t continue to refresh. Please help me, thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:51:41+00:00Added an answer on June 13, 2026 at 4:51 am

    When a page is refreshed, the setInterval handle will be gone, so it will not work as you expected.

    A solution is to do setInterval inside your extension:

    chrome.contextMenus.onClicked.addListener(function(tab) {
        setInterval(function() {
            chrome.tabs.executeScript(tab.id, {
                code: "location.reload()"
            });
        }, 2000);
    });
    
    var id = chrome.contextMenus.create({
        "title": "Auto Refresh",
        "contexts": ["page"]
    });​
    

    As I acknowledge that you want to refresh only the current page, I added tab.index as the first parameter to chrome.tabs.executeScript so that only the current page is being refreshed.

    Beware that the user may choose the option more than once, so you may need to check if it is the case or remove the context menu item. Also when the tab is closed you will need to call clearInterval, or else you will cause a lot of problems. These are left for you to practice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in my background script: chrome.tabs.onUpdated.addListener(function(tabId, changeinfo, tab) { if
I have this piece of code in my background.js: chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { if
I want to trigger the click, to which following code is listening: chrome.browserAction.onClicked.addListener(function(tab) {});
i have the following code which switches some fullscreen-background-images (fadeOut, fadeIn). setInterval(function() { var
The following code with alternating background I have shows up fine in Chrome and
I have this code snippet: $(function() { $('.toolbar [id^=button]').on('click', function () { $(this) .css('background-color',
I have this javascript code: if (navigator.userAgent.toLowerCase().indexOf(chrome) >= 0) { $(window).load(function() { $('input:-webkit-autofill').each(function() {
i need to set image as background in android gridview. I have code which
I have some code that will change the background color of a specific label
I have the following code a.rollover { background-image: url('sprite.jpg'); display: block; width: 191px; height:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.