-1

I have a function that will reload the current page after a period of time. I want this function to run automatically every time the page is reloaded (using the debugger).

function reloadPage() {     
   window.location.reload(false);  
}
setInterval(reloadPage,3000)

The problem is that every time the page is reloaded, the code in the debugger will be cleaned and the function will not be called. How to fix this?

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
shinobitiger310
  • 175
  • 1
  • 7

1 Answers1

0

A very simple solution: Rather than putting the Javascript into the console, consider putting it in your application but disabling it when you're not debugging.

For example, you could have a GET parameter in your URL that, when present, triggers the function. A good explanation of how to retrieve a GET parameter in Javascript is at How to retrieve GET parameters from javascript?

An even simpler alternative would be to simply leave this code commented out, and comment it in when you want to debug. (This is not a good practice and I will scold you for it during code review, but it is a real thing that real people do, and it has the advantage of being easy and working.)

-

An alternative: You could detect when the console is open, and only run your code when the console is detected (though this would annoy power users like me who tend to always have developer tools open). It's not trivial to detect, but there's a library you can use: https://github.com/zswang/jdetects

Community
  • 1
  • 1
AmericanUmlaut
  • 2,817
  • 2
  • 17
  • 27
  • Thanks for your quick reply. Actually i don't want to debug the code, my real purpose is i want to reload a page REPEATLY in internet explorer for a period of time (this page can only be loaded in internet explorer and i can not change its source code) so i write a javascript for this in debugger. Could you please give another solution for this. Thanks. – shinobitiger310 Feb 08 '17 at 04:29
  • My answer *is* a solution for that. I'm describing three ways that you can run the code without having it in the debugger. Code written into the debugger is lost on page reload - any of these solutions solves that for you. – AmericanUmlaut Feb 08 '17 at 04:36