Skip to content

Instantly share code, notes, and snippets.

@Ray33
Last active January 24, 2018 07:10
Show Gist options
  • Save Ray33/c14a65a761cdb72f1663f19cec961c25 to your computer and use it in GitHub Desktop.
Save Ray33/c14a65a761cdb72f1663f19cec961c25 to your computer and use it in GitHub Desktop.
Visibility call via HTML
//initialize variables
var visible_urls = new Map();
var fired_urls = [];
//When news item is created, add it's id and the it's visibilityUrl to map
visible_urls.set(news_item.id, news_item.visibleUrl);
//call the url in background
function open_visible_url(url) {
if (url){
if (!url.startsWith(protocol)){
url = url.replace("http", protocol);
}
$.getJSON(url,
function () {
console.log("visible url called:" + url);
}
);
}
}
//Select all items and append the visbility url call on visible.
// This should be called after all items are rendered (on each call to the API)
$(".class_selector_with_news").each(function () {
//activate the waypoint library to trigger on visible
$("#" + this.id).waypoint({
handler: function (direction) {
if (jQuery.inArray(visible_urls.get(this.id), fired_urls) == -1) {
open_visible_url(visible_urls.get(this.id));
fired_urls.push(visible_urls.get(this.id));
}
},
offset: "bottom-in-view"
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment