I have a simple script that opens a newsletter signup form when scrolling down or when clicking on a subscribe button.
I want to be able to disable the on scroll (ONLY) for about 10 minutes the user is on the website to stop irritating them with a script that opens every time they scroll. How do I have to modify the script below?
// newsletter widget on screens bigger than 680px
if ($(window).width() > 680 ) {
$('.subscribe-btn a').click(function(e){
e.preventDefault();
$("#newsletter_widget").show("slow")
});
// newsletter widget
var $win = $(window);
var $doc = $(document);
var $widget = $('#newsletter_widget');
var $close = $('.close-widget');
function scrollHandler () {
if ($doc.scrollTop() >= $doc.height() / 5) {
$widget.show('slow')
}
}
$win.scroll(scrollHandler);
$close.click(function () {
$widget.hide('slow');
$win.off('scroll', scrollHandler);
// set cookie on close for 5 minutes before the widget can open again
});
}
When they close the newsletter window create cookie and then perform check if certain time has passed based on time that you inserted in cookie, if it is passed open newsletter window again othervise do nothing.
If anyone ever needs a similar solution, here is something logically correct and similar in terms of logic to my final script:
if ($(window).width() > 680 ) {
$('.subscribe-btn a').click(function(e){
e.preventDefault();
$("#newsletter_widget").show("slow");
});
// newsletter widget
var $win = $(window);
var $doc = $(document);
var $widget = $('#newsletter_widget');
var $close = $('.close-widget');
function scrollHandler () {
if ($doc.scrollTop() >= $doc.height() / 5) {
$widget.show('slow')
}
}
var cookieValue = jQuery.cookie("thatcookiethatmakesmesohungry");
if (cookieValue) {
// nothing to see here
}else{
// execute newsletter widget
$win.scroll(scrollHandler); // alert("no cookie");
}
$close.click(function () {
$widget.hide('slow');
$win.off('scroll', scrollHandler);
// set cookie on close for 5 minutes before the widget can open again
var date = new Date();
date.setTime(date.getTime() + (5*60*1000)); // 5 minutes
$.cookie('test', 1, { expires: date });
});
}