简体   繁体   English

JQuery或PHP仅执行一次,然后等待设置的时间段再执行一次。

[英]JQuery or PHP execute only one time and then wait set period of time before executing again.

Here is the problem.. 这是问题所在。

I am trying to make a pop up execute only 1 time for a user every 30 days. 我正在尝试使用户每30天仅执行一次弹出窗口。 Currently the pop up is being executed in an infinite loop.. every time I trigger it executes.. over and over and over again.. It should only happen 1 time and then set a cookie of some sort or a check and 30 days later if the user comes back it will execute again. 目前,该弹出式窗口正在无限循环中执行..每次触发执行时..一遍又一遍..它应该只发生1次,然后设置某种cookie或支票,并在30天后如果用户回来,它将再次执行。 I have tried setting cookie in PHP.. Using the one. 我试过在PHP中设置cookie。 JQuery method.. setting cookie in Jquery.. using a flag variable.. all these things and it still just executes alwayse. JQuery方法..使用标志变量在Jquery ..中设置cookie。.所有这些事情,它仍然只执行alwayse。 I need solution in PHP or Jquery preferably. 我最好在PHP或Jquery中需要解决方案。

Your help is much appreciated. 非常感谢您的帮助。 Thank you. 谢谢。

Here is code 这是代码

 <script> $( document ).ready(function() { var SetWait; var filledPhone; //setTimeout(function(){}, 1500);//close timeout SetWait = 1; setTimeout(function(){SetWait=0;}, 1200); $("body").mouseleave(function() { if(SetWait==0){ if(!filledPhone){ $("#myModalpopup").fadeIn(); $(".modal-backdrop").fadeIn(); $(".modal-content").fadeIn(); }//close filled email check } });//close function $(".close").click(function() { SetWait = 1; $("#myModalpopup").fadeOut(); $(".modal-backdrop").fadeOut(); $(".modal-content").fadeOut(); setTimeout(function(){SetWait=0;}, 2500); });//close function $("#twillioPopup").click(function() { var the_number = $("#custPhone").val(); //email user enters if(!the_number){ alert('Please enter phone number.'); }else{ $("#myModalpopup").fadeOut(); $(".modal-backdrop").fadeOut(); $(".modal-content").fadeOut(); filledPhone = the_number; ///////// DO AJAX TO SAVE EMAIL AND CART ID ETC ///////// $.ajax({ type: 'POST', url: 'ajax/twillioPopup.php', data: {the_number:the_number}, success: function( response ){ $('#success_message').html(response); } }); //close ajax ///////// DO AJAX TO SAVE EMAIL AND CART ID ETC ///////// }//close if else });//close click });//close ready </script> 

Try to create a php COOKIE that expires after 30 days. 尝试创建30天后过期的php COOKIE。 If the cookie is not set, then run the code and set it, else do nothing. 如果未设置cookie,则运行代码并进行设置,否则不执行任何操作。 Something like this? 像这样吗

<?php

if(isset($_COOKIE['yourCookieName'])){
   //Do nothing
} else {
   $cookie_name = "yourCookieName";
   $cookie_value = "set";
   setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
   ?>
    //Break out of php and run your javascript here
   <?php 
}
?>

This should detect if the cookie is set, and if its not, then run the javascript once and then set the cookie (if they revisit in the next 30 days whilst the cookie is still active, the javascript will be ignored) 这样可以检测是否设置了cookie,如果没有,则运行一次javascript,然后设置cookie(如果在cookie仍处于活动状态的情况下在接下来的30天内重新访问它们,则将忽略javascript)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM