javascript - Need to call script every second, or better solution? -


this script:

        <script>             function getcookie(cname) {                 var name = cname + "=";                 var decodedcookie = decodeuricomponent(document.cookie);                 var ca = decodedcookie.split(';');                 (var = 0; < ca.length; i++) {                     var c = ca[i];                     while (c.charat(0) == ' ') {                         c = c.substring(1);                     }                     if (c.indexof(name) == 0) {                         return c.substring(name.length, c.length);                     }                 }                 return "";             }              var time_popup = <?php echo $time_popup; ?>;             $(document).ready(function () {                     $.magnificpopup.open({                         items: {                             src: '#recently-viewed-popup'                         },                         type: 'inline',                         callbacks: {                             close: function closepopup() {                                 var close = new date();                                 close.setminutes(close.getminutes() + time_popup);                                 document.cookie = "close_popup=" + close;                             }                         }                     });             });              var time_now = new date();             var popup_close = getcookie('close_popup');              if (time_now - time_popup == popup_close) {                 $.magnificpopup.open({                     items: {                         src: '#recently-viewed-popup'                     },                     type: 'inline',                     callbacks: {                         close: function closepopup() {                             var close = new date();                             close.setminutes(close.getminutes() + time_popup);                             document.cookie = "close_popup=" + close;                         }                     }                 });             }         </script> 

i have popup. have var time_popup comes admin, choose minutes show popup again (after closed). when popup closed setcookie time + minutes admin, when want popup again.

the logic simple:

time - time_popup == last_time_close_popup

but how check every second, logic in last if.

you use javascripts settimeout() method

that way don't need check every second see if 5 minutes has past. set timeout run function pass first argument after time in milliseconds passed in second argument.

settimeout(function(){   // logic }, 300000); 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -