0

I have a webpage and it has a Refresh Button. I need to click the button every 5 minutes. The Normal Refresh or Reload or F5 doesn't work in this situation. Is there a way that Javascript can do this task.

Like, I will save the javascript as Bookmark and once I click the bookmark. Then, the javascript event has to click the refresh button every 5 minutes.

I googled it and I found the below code. But, it doesn't work. When I click on it, it just showing a random number in a blank page.

javascript:if(window.autoRefreshInterval) { clearInterval(window.autoRefreshInterval); };
window.autoRefreshInterval = setInterval(function() { jQuery(".refresh").click(); },60000)

thank you in advance,

arielb
  • 392
  • 3
  • 10
rocky09
  • 113
  • 1
  • 11

5 Answers5

1

"I have a webpage and it has a Refresh Button. I need to click the button every 5 minutes. The Normal Refresh or Reload or F5 doesn't work in this situation. Is there a way that Javascript can do this task."

It's not very clear to me, but every time you refresh a webpage, javascript is loaded again. So if you have intervals or variables they are reset at each refresh. If you want to keep some value among refreshs you can store values using localStorage or cookies for example. If you want refresh automatically page you can use setInterval or metatag "refresh".

"Like, I will save the javascript as Bookmark and once I click the bookmark. Then, the javascript event has to click the refresh button every 5 minutes."

Look at this: Add a bookmark that is only javascript, not a URL

Marcelo Vismari
  • 1,147
  • 7
  • 15
0

you can call your refresh code function or button click event in

setTimeout(yourFucntion(),5000); 
else
setTimeout($("#btnName").click(),5000);
Piyush
  • 21
  • 1
  • 9
0

There are two versions for you to try, one uses javascript to click the button the other automates running the function that they have tied to the button.

Non jQuery:

 javascript:(function(){if(window.autoRefreshInterval) {clearInterval(window.autoRefreshInterval);}window.autoRefreshInterval = setInterval(function(){document.getElementsByClassName("refresh")[0].addEventListener('click');},60000);})()

Or with jQuery (OP's comment on original thread):

 javascript:(function(){if(window.autoRefreshInterval) {clearInterval(window.autoRefreshInterval);}window.autoRefreshInterval = setInterval(function(){$ctrl.refresh();},60000);})()
imvain2
  • 15,480
  • 1
  • 16
  • 21
0

Try below code:

<!DOCTYPE html>
<html>
<body  onload="f1()">
<script language ="javascript" >
        var tmp;
        function f1() {
             tmp = setInterval(() => f2(), 2000); // replace this with 5 min timer
        } 
        function f2() {
            document.getElementById("Button1").click();
        }

        function f3() {
          console.log("Hello World");
        }
</script>

<button id="Button1" onclick="f3()">click me</button>
<div id="demo"></div>
</body>

</html>
Praveen Kishor
  • 2,413
  • 1
  • 23
  • 28
  • hi, i just want a code to save as Bookmark. So, I click it and it will refresh every 5 minutes. – rocky09 Feb 15 '19 at 17:32
0

Delayed post, but hopefully it helps someone :-).

The trick for me was locating the element by css document.querySelector('.pbi-glyph-refresh').click();

You can combine this with the original code like so, it correctly clicks the PowerBI refresh button on a 60 second timer (the var is in ms).

javascript:if(window.autoRefreshInterval) { clearInterval(window.autoRefreshInterval); };
window.autoRefreshInterval = setInterval(function() {document.querySelector('.pbi-glyph-refresh').click(); },60000)