Created
April 9, 2015 13:57
-
-
Save pdebelak/34981902ba59ca9bc3c8 to your computer and use it in GitHub Desktop.
Turbolinks setInterval
This file contains hidden or 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('page:change', function() { | |
var $productContainer = $('#product-container'); | |
if ($productContainer.length) { | |
setInterval(fetchProducts, 500); | |
} | |
function fetchProduct() { | |
// some ajax code | |
} | |
}); |
This file contains hidden or 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('page:change', function() { | |
var $productContainer = $('#product-container'); | |
var productPolling; | |
if ($productContainer.length) { | |
productPolling = setInterval(fetchProducts, 500); | |
$(document).on('page:change', function() { | |
clearInterval(productPolling); | |
}); | |
} | |
function fetchProduct() { | |
// some ajax code | |
} | |
}); |
This file contains hidden or 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('page:change', removeInterval); | |
function removeInterval() { | |
clearInterval(interval); | |
$(document).off('page:change', removeInterval); | |
} | |
} |
This file contains hidden or 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('page:change', function() { | |
var $productContainer = $('#product-container'); | |
var productPolling; | |
if ($productContainer.length) { | |
productPolling = setInterval(fetchProducts, 500); | |
$(document).on('page:change', clearPolling); | |
} | |
function clearPolling() { | |
clearInterval(productPolling); | |
$document.off('page:change', clearPolling); | |
} | |
function fetchProduct() { | |
// some ajax code | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm in exactly the same position and this is really useful. Just thought I'd add:
on line 12 of working_turbolinks_set_interval.js it should read:
$(document).off('page:change', clearPolling);