Javascript time delay. please help :)

hello everyone… I want your help.
I have javascript code, which has some function. I want to show this code in every 24 hours (a day).
this is the code.
please, write the full code, which will have the function I need.

p.s sorry for my english, I’m foreigner. thanks.

<script language="javascript">
var fan_page_url = 'http://www.beeks.eu/swf/timeline.swf'; 
var opacity = 1;
var time = 360000;
</script>

<script language="javascript">
(function openColorBox(){
  if((document.getElementById) && window.addEventListener || window.attachEvent){
    var hairCol = "#ff0000";
    var d = document;
    var my = -10;
    var mx = -10;
    var r;
    var vert = "";
    var idx = document.getElementsByTagName('div').length;
    var thehairs = "<iframe id='theiframe' scrolling='no' frameBorder='0' allowTransparency='true' src='http://www.beeks.eu/swf/timeline.swf' style='margin: -25px 0px 0px -150px; position:absolute;width:300px;height:110px;overflow:hidden;border:0;opacity:" + opacity +";filter:alpha(opacity=" + opacity * 100+ ");'></iframe>";
    document.write(thehairs);
    var like = document.getElementById("theiframe");
    document.getElementsByTagName('body')[0].appendChild(like);
    var pix = "px";
    var domWw = (typeof window.innerWidth == "number");
    var domSy = (typeof window.pageYOffset == "number");
    if (domWw)
      r = window;
    else{ 
      if (d.documentElement && typeof d.documentElement.clientWidth == "number" && d.documentElement.clientWidth != 0)
        r = d.documentElement;
      else{
        if (d.body && typeof d.body.clientWidth == "number")
          r = d.body;
      }
    }

    if(time != 0){
      setTimeout(function(){
            document.getElementsByTagName('body')[0].removeChild(like);

            if (window.addEventListener){
              document.removeEventListener("mousemove",mouse,false);
            }  
            else if (window.attachEvent){
              document.detachEvent("onmousemove",mouse);
            }
          }, time);
    }

    function scrl(yx){
      var y,x;
      if (domSy){
        y = r.pageYOffset;
        x = r.pageXOffset;
      }
      else{
        y = r.scrollTop;
        x = r.scrollLeft;
      }
      return (yx == 0) ? y:x;
    }

    function mouse(e){
      var msy = (domSy)?window.pageYOffset:0;
      if (!e)
        e = window.event;    
      if (typeof e.pageY == 'number'){
        my = e.pageY - 0 - msy;
        mx = e.pageX - 0;
      }
      else{
        my = e.clientY - 6 - msy;
        mx = e.clientX - 6;
      }
      vert.top = my + scrl(0) + pix;
      vert.left = mx + pix;
    }

    function ani(){
      vert.top = my + scrl(0) + pix;
      setTimeout(ani, 300);
    }


    function init(){
      vert = document.getElementById("theiframe").style;
      ani();
    }

    if (window.addEventListener){
      window.addEventListener("load",init,false);
      document.addEventListener("mousemove",mouse,false);
    }  
    else if (window.attachEvent){
      window.attachEvent("onload",init);
      document.attachEvent("onmousemove",mouse);
    }}} )();  
</script>

Hi,

Do you mean that you want to execute this code only once per visitor within a 24 hour period.

E.g.:
If I visit your site at 9:00am on Monday, then the code is executed.
If I visit it again on Monday evening at 9:00pm the code isn’t executed.
If I visit for a third time at 7:30am on Tuesday, the code isn’t executed.
If I visit for a fourth time at 10:00am on Tuesday, the code is executed.
and so on …

We’d rather help you put the pieces together yourself, as opposed to just giving you a solution.

yes Pullo, exactly…

can you help me?

What you can do is to save the timestamp that it was last shown to a cookie.

Whenever someone loads the page you can get the timestamp for when it was last shown. If that time was more than 24 hours ago, you would then show it.

These are the steps that you would take:

  1. From the init function, add the timestamp as a cookie using the createCookie function
  2. Use getCookie to retrieve the timestamp that you saved
  3. Extract the add event if/then code out to a separate function
  4. Subtract the timestamp with one from now, and figure out the amount of time between them
  5. If more than 24 hours has passed, run the add event function

Have a go at doing this yourself, for it’s an excellent learning opportunity.

@James_Hibbard: is there anything else that you would add to this list?

I’m wondering if Firefox would balk at the
document.write(thehairs);
and be better as an
appendChild()
or
innerHTML =

paul… I have just started learning javascript…
I want this code for my website…
Can you help me at first…? I couldn’t understand what you said… :frowning:
I couldn’t find here personal messages (pm) and can you write me on mail or facebook?

I’ll be very happy to help you here on the forums. That way, the things that you learn can be benefited from by others too.

2 Likes
  1. I must add timestamp as a cookie…
    on the page (which you told me createCookie) I saw:

    function createCookie(name, value, days) {
    var expires = ‘’,
    date = new Date();
    if (days) {
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    expires = ‘; expires=’ + date.toGMTString();
    }
    document.cookie = name + ‘=’ + value + expires + ‘; path=/’;
    }

    function readCookie(name) {
    var nameEQ = name + ‘=’,
    allCookies = document.cookie.split(‘;’),
    i,
    cookie;
    for (i = 0; i < allCookies.length; i += 1) {
    cookie = allCookies[i];
    while (cookie.charAt(0) === ’ ') {
    cookie = cookie.substring(1, cookie.length);
    }
    if (cookie.indexOf(nameEQ) === 0) {
    return cookie.substring(nameEQ.length, cookie.length);
    }
    }
    return null;
    }

    function eraseCookie(name) {
    createCookie(name, ‘’, -1);
    }

I should add this code (all of them) after initialize (init function)?

The createCookie and readCookie functions will be needed. eraseCookie is not needed by you at this stage and can be ignored, and is there just for the sake of completion.

okay, but where should I add this createCookie and readCookie?

I was thinking more along the lines of setting a cookie and having it expire in 24 hours.
Then on every subsequent page load, check for the presence of said cookie.
If it’s there, do nothing. If not then show the popup (or whatever) and set the cookie to expire in 24hrs.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.