Forked from pdebelak/failed_turbolinks_set_interval_1.js
Last active
September 26, 2018 14:36
-
-
Save btrewern/0d81711a00734bd7c88cf0b3f5827555 to your computer and use it in GitHub Desktop.
Turbolinks setInterval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).on('turbolinks:load', function() { | |
var $productContainer = $('#product-container'); | |
if ($productContainer.length) { | |
setInterval(fetchProducts, 500); | |
} | |
function fetchProduct() { | |
// some ajax code | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).on('turbolinks:load', function() { | |
var $productContainer = $('#product-container'); | |
var productPolling; | |
if ($productContainer.length) { | |
productPolling = setInterval(fetchProducts, 500); | |
$(document).on('turbolinks:load', function() { | |
clearInterval(productPolling); | |
}); | |
} | |
function fetchProduct() { | |
// some ajax code | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function turbolinksSetInterval(intervalFunction, seconds) { | |
var interval = setInterval(intervalFunction, seconds); | |
$(document).on('turbolinks:load', removeInterval); | |
function removeInterval() { | |
clearInterval(interval); | |
$(document).off('page:change', removeInterval); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).on('turbolinks:load', function() { | |
var $productContainer = $('#product-container'); | |
var productPolling; | |
if ($productContainer.length) { | |
productPolling = setInterval(fetchProducts, 500); | |
$(document).on('turbolinks:load', clearPolling); | |
} | |
function clearPolling() { | |
clearInterval(productPolling); | |
$(document).off('turbolinks:load', clearPolling); | |
} | |
function fetchProduct() { | |
// some ajax code | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment