0

I try to run a loop method on a web page (Facebook) like this:

function loopRefresh () {
  console.log("loop refresh method call");
  if (shallRefresh){
      setTimeout(function(){
        loopRefresh()
        //Do something here
      }, 5000);
  } else {
      setTimeout(function(){
        loopRefresh()
        //Do something different here
      }, 5000);
  }
}

Now so far so good, everything works and the method is called every 5th second. The problem is that when the user clicks the home button: Facebook Home Button Code

the page gets reloaded because of the anchor tag and the href, even though it does not refer to a new page. This breaks the loop.

I already added this function as onclick event to the Home Button:

function homeRefresh() {
  shallRefresh = true;
//setTimeout(function(){
//  Do the same thing here as in the true case in the loopRefresh method
// }, 2000);
}

I originally just wanted to make the setTimeout call in here, so that the callback function gets executed after the user clicked the button, without the loopRefresh method. But I thought I could solve the problem by passing the variable, which also doesn't work.

Lennie
  • 253
  • 6
  • 16
  • Based on that screenshot, you are referencing a fully qualified url. When clicked by a user, it will refresh the page... – Chris May 30 '18 at 03:31
  • okay, so is there like an "onrefresh" event that I can use to trigger a function call after that refresh? – Lennie May 30 '18 at 03:35

1 Answers1

1

Use event.preventDefault() on function starting. like,

function homeRefresh() {
  event.preventDefault(); // here..

  shallRefresh = true;
  //setTimeout(function(){
  //  Do the same thing here as in the true case in the loopRefresh method
  // }, 2000);
}

event.preventDefault() will prevent the default action of the hyperlink.

NOTE: You cannot continue a javascript function after a page reload.

ctrleffive
  • 126
  • 6
  • thanks a lot! so the way to execute a function after that realod would be , yes? That is the only way? – Lennie May 30 '18 at 03:45
  • 1
    basically it is not possible. when a page is reloaded, its entire asset files are also reloaded. so is the javascript. browser will stop everything (every running functions, loops, watchers, etc.) when a page is navigated away. but nothing is impossible. you can set a unique local storage variable before page reload and cross verify it after reload. – ctrleffive May 30 '18 at 03:50
  • onload will get executed after the page load. not reload. – ctrleffive May 30 '18 at 03:56
  • Okay thanks for the input about the unique local storage variables, will look into this. But I think the page is not fully reloaded. I load the code via an Add-On for Firefox and when I refresh the page with ctrl + r my code is reloaded and the loopRefresh function runs again. If I click on the button it is not. There has to be two different reloads, lol. – Lennie May 30 '18 at 03:58
  • i think you are talking about ajax reloads. specific areas of a website can be changed by using ajax methods. – ctrleffive May 30 '18 at 04:01
  • I'm not familiar with these, how can I make sure that the loop method is called after one of these ajax reloads is executed, or is that also not possible? – Lennie May 30 '18 at 04:05
  • ajax requests have callbacks. but finding the function which performs the request from code like Facebook's is a headache. or you can use jquery to catch all ajax requests in a page. https://stackoverflow.com/a/287212/2249193 are you developing an extension? – ctrleffive May 30 '18 at 04:13
  • yes I do. Indeed it would be, but the way to search for the right ajax call also doesn't sound easy and I don't wanna screw up any native Facebook code...I just need to make sure after someone clicked the Home button and some part of the page is reloaded that I can trigger my function. I think I might use MutationObservers and just look up the value of my variable that I can set when the Home Button was clicked. – Lennie May 30 '18 at 04:18
  • Tried that, but without using setTimeout I cannot trigger something after that reload....even with the MutationObserver it doesn't work...is there any event that I can observe? – Lennie May 30 '18 at 21:23
  • don’t know ☹️ I'm helpless. – ctrleffive May 31 '18 at 16:46