Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is an improved version of https://stackoverflow.com/a/5835533/1175714
- function slideLoop(currMarg, selector){
- let contStyle = document.querySelector(selector).style;
- return function(){
- currMarg = (currMarg == 1800) ? (0) : (currMarg + 600);
- contStyle.marginLeft = "-%dpx".replace("%d", curMarg);
- }
- }
- function slide() {
- return setInterval(slideLoop(0, "#container"), 3000);
- }
- // Notes:
- // Use document.querySelector(css_selector) instead of document.getElementById(id)
- // Always track setInterval(fn, interval)'s return value so it can later be closed with clearInterval(interval_id)
- // Put the interval's callback into its own formal function for readability and maintainability
- // If state is kept with the callback, the callback should be returned from another function (slideLoop) to form a closure.
- // Use proper whitespace. Logical blocks that do different things should be separated by one empty line
- // Avoid "string" + value + "string" + ... Make a format function or use String.prototype.replace(string/regex, new_string)
- // Always surround the arguments of your ternary expressions with parens. It aids readability and prevents bugs.
Add Comment
Please, Sign In to add comment