Jump to content
  • entries
    3
  • comments
    0
  • views
    519

отложенная модалка на джсе

kJlukOo

247 views

var delay_sec = 10 * 60; // 10 min

function init_delayed_modal()
{
	var first_visit_time = get_cookie('first_visit_time');
	var current_time = new Date().getTime() / 1000;
	var time_passed;

	if(first_visit_time == undefined)
	{
		// first time visit
		create_cookie('first_visit_time', current_time);
		setTimeout(show_modal,delay_sec * 1000);
	}
	else 
	{
		// two and more time visit
		time_passed = current_time - first_visit_time;

		//if time left
		if(time_passed>delay_sec)
			show_modal();
		else
			setTimeout(show_modal,(delay_sec - time_passed) * 1000);
	}
}

function show_modal()
{
	// set cookie and show modal
	create_cookie('modal_alredy_show', true);
	alert('modal');
}

//https://stackoverflow.com/questions/1458724/how-do-i-set-unset-a-cookie-with-jquery
function get_cookie(name) 
{
	var matches = document.cookie.match(new RegExp(
		"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\/\+^])/g, '\$1') + "=([^;]*)"
		));
	return matches ? decodeURIComponent(matches[1]) : undefined;
}

//https://stackoverflow.com/questions/1458724/how-do-i-set-unset-a-cookie-with-jquery
function create_cookie(name, value, days) 
{
	var expires;

	if (days) 
	{
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		expires = "; expires=" + date.toGMTString();
	} 
	else 
		expires = "";

	document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/";
}

if(get_cookie('modal_alredy_show') == undefined)
	init_delayed_modal();

 

допустим нам необходимо однократно показать юзеру сообщение спустя 10 минут нахождения на сайте. чистый джс



0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.