By deciding to refresh the ad only when it is in view, you’ve taken the right step. Reloading ads without considering ad viewability can affect your CPM rates and domain viewability substantially. In general, there are two ways to do it.
a. Use a header bidding wrapper tech from a third party that will let you refresh ads based on viewability. For instance, our product “Active Exposure Time” track ad viewability and user activity on the page to refresh ads optimally.
b. Use ImpressionViewableEvent to refresh ads. This event is fired when an impression becomes viewable, according to the Active View criteria. You can use this JS fiddle code for reference and Google’s documentation for further clarification on ImpressionViewableEvent.