Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-05-2006, 06:07 PM   PM User | #1
kimstanely
New Coder

 
Join Date: Apr 2005
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
kimstanely is an unknown quantity at this point
Exclamation NEED Urget Help With Timer

Hi,


Is there a way that i can update something, after 2 weeks, i think we can do this by using timer but i am having a little problem doing that. Can some ont please help!!!!

Thanks
Kim

Last edited by kimstanely; 05-09-2006 at 05:30 PM..
kimstanely is offline   Reply With Quote
Old 05-05-2006, 06:41 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
We need more information to give a helpful reply.

What do you mean "update something after one day". Update what? Every day or just once?

I am sure that one of our onts will assist you!
Philip M is offline   Reply With Quote
Old 05-05-2006, 06:51 PM   PM User | #3
kimstanely
New Coder

 
Join Date: Apr 2005
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
kimstanely is an unknown quantity at this point
Internet Explorer

See this peice of code changes an image hyperlink and alt tag after evey 1 seconds i want it to change after evey i day, the problem is that when i close the window evything starts from beginning i dont want it to do that:

PHP Code:


<HTML><HEAD>

<
SCRIPT LANGUAGE="JavaScript">

var 
timer null
var     counter =0
function showImage() {
    
timer=setTimeout("showImage()"10000);
    
counter++;

    if (
counter >= pic.length
        
counter 1;
    
document.pictures.src pic[counter].src;
    
elementA=document.getElementById('image2'); 
    
elementA.setAttribute('alt'alttextArray[counter]);
    var 
textA summaryArray[counter];
    
document.getElementById('text').innerHTML textA;
    
document.getElementById('links').href linkArray[counter];
}    

if (
document.images) {

    
pic = new Array()

    
pic[0] = new Image(108108);
    
pic[0].src "flag_col.jpg";

    
pic[1] = new Image(108108);
    
pic[1].src "books_glasses.jpg";

    
pic[2] = new Image(108108);
    
pic[2].src "lawyers_steps.jpg";

    
pic[3] = new Image(108108);
    
pic[3].src "pen_book.jpg";

    
pic[4] = new Image(108108);
    
pic[4].src "scales1.jpg";
    
    
pic[5] = new Image(108,108);
    
pic[5].src 'ccs.gif'
    
    
pic[6] = new Image(108,108);
    
pic[6].src 'sj_e.gif'
    
}
var 
alttextArray = new Array()
alttextArray[0] = 'This 1'
alttextArray[1] = 'This 2'
alttextArray[2] = 'This 3'
alttextArray[3] = 'This 4'
alttextArray[4] = 'This 5'
alttextArray[5] = 'This 6'
var summaryArray = new Array()

summaryArray[0] = 'Lalal    '
summaryArray[1] = 'a'
summaryArray[2] = 'b'
summaryArray[3] = 'c'
summaryArray[4] = 'd>'
summaryArray[5] = 'e'
summaryArray[6] = 'f'

var linkArray = new Array()
linkArray[0] = 'www.yahoo.com'
linkArray[1] = 'www.hotmail.com'
linkArray[2] = 'www.yahoo.com'
linkArray[3] = 'www.yahoo.com'
linkArray[4] = 'www.yahoo.com'
linkArray[5] = 'www.yahoo.com'
linkArray[6] = 'www.yahoo.com'

  
document.write('<td height="65" valign="top" cellpadding="2" border="1">')
  
document.write('<IMG SRC="flag_col.jpg" WIDTH="108" HEIGHT="108" NAME="pictures" id="image2" alt="What">');  
  
document.write('</td>')
  
document.write('<p>')
  
document.write('<a href="" id="links">')
  
document.write('<div id="text">  </div>')
  
document.write('</a>');

// - End of JavaScript - -->
</SCRIPT>
</HEAD>

<BODY BACKGROUND="FFFFFF">


 <script type="text/javascript" language="javascript">
      showImage();
    </script>
</BODY>
</HTML> 

Thanks in advance Sorry i did not post this before

Kim

Last edited by kimstanely; 05-05-2006 at 06:53 PM..
kimstanely is offline   Reply With Quote
Old 05-05-2006, 07:32 PM   PM User | #4
boxxer03
Regular Coder

 
Join Date: Sep 2005
Location: Chicago, IL
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
boxxer03 is an unknown quantity at this point
You won't be able to do it with a timer, because a timer only runs while a user is on the page and then resets once they leave. The only way I could think of doing it would be to use the date, but you would have to find a way to relate the date to a picture.
__________________
I'm only telling you how I would do it, not how its supposed to be done. ;)
boxxer03 is offline   Reply With Quote
Old 05-08-2006, 09:03 PM   PM User | #5
Beagle
Senior Coder

 
Join Date: Jul 2005
Location: New York, NY
Posts: 1,084
Thanks: 4
Thanked 19 Times in 19 Posts
Beagle is an unknown quantity at this point
ok, so here's a decent enough idea.

Get all your images into an array.

Then, get the day of the year.

Then use modulo on the length of the array and day of year to determine your array index.

Code:
var pic;

if (document.images)
{
    pic = new Array()

    pic[0] = new Image(108, 108);
    pic[0].src = "flag_col.jpg";

    pic[1] = new Image(108, 108);
    pic[1].src = "books_glasses.jpg";

    pic[2] = new Image(108, 108);
    pic[2].src = "lawyers_steps.jpg";

    pic[3] = new Image(108, 108);
    pic[3].src = "pen_book.jpg";

    pic[4] = new Image(108, 108);
    pic[4].src = "scales1.jpg";
    
    pic[5] = new Image(108,108);
    pic[5].src = 'ccs.gif'
    
    pic[6] = new Image(108,108);
    pic[6].src = 'sj_e.gif'
}

function getImageByDayOfYear(p_day)
{
  if (pic.length && pic.length > 0)
  {
    return pic[(p_day - 1)%pic.length];
  }
  else
  {
     return null;
   }
}

Date.prototype.getDayOfYear = function() {
  var Y = this.getFullYear(), M = this.getMonth(), D = this.getDate();
  return (Date.UTC(Y, M, D) - Date.UTC(Y, 0, 0)) / 86400000;
};

var doy = new Date();
var imageToDisplay = getImageByDayOfYear(doy.getDayOfYear());
DOY code from: http://www.merlyn.demon.co.uk/js-date8.htm#DoY
Beagle is offline   Reply With Quote
Old 05-09-2006, 01:27 PM   PM User | #6
kimstanely
New Coder

 
Join Date: Apr 2005
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
kimstanely is an unknown quantity at this point
hi Beagle,

When i placed this code in the head it doesn't do anything. is there something i need to do?

Thanks for the help,
Kim
kimstanely is offline   Reply With Quote
Old 05-09-2006, 07:43 PM   PM User | #7
kimstanely
New Coder

 
Join Date: Apr 2005
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
kimstanely is an unknown quantity at this point
Hi Beagle,

Can i do this by using asp, if it can , could you please show me a little example.

Thanks
kim
kimstanely is offline   Reply With Quote
Old 05-09-2006, 07:48 PM   PM User | #8
Beagle
Senior Coder

 
Join Date: Jul 2005
Location: New York, NY
Posts: 1,084
Thanks: 4
Thanked 19 Times in 19 Posts
Beagle is an unknown quantity at this point
What do you mean it doesn't work?

If you want to paste the errors you get, we can help you from there.

As for ASP, I'm more experienced in PHP, better ask once you get the javascript down in the ASP forum.
Beagle is offline   Reply With Quote
Old 05-09-2006, 08:46 PM   PM User | #9
boxxer03
Regular Coder

 
Join Date: Sep 2005
Location: Chicago, IL
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
boxxer03 is an unknown quantity at this point
Quote:
Originally Posted by kimstanely
Hi,


Is there a way that i can update something, after 2 weeks, i think we can do this by using timer but i am having a little problem doing that. Can some ont please help!!!!

Thanks
Kim
I seen that you edited your first post from daily to bi-weekly (you can see the last edited tag under your post). I would suggest that if you want to get help that is relevant to what you wanna do that you decide for sure how often the picture needs to change. Because you have another post on here for the same thing, but changing the pictures after a certain amount of seconds.
__________________
I'm only telling you how I would do it, not how its supposed to be done. ;)
boxxer03 is offline   Reply With Quote
Old 05-10-2006, 01:11 PM   PM User | #10
kimstanely
New Coder

 
Join Date: Apr 2005
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
kimstanely is an unknown quantity at this point
Hi beagle,

Could you please let me know where should i place the Script you gave.
Because when i copy the code you gave and pasted it in my file there was no error but the page was blank

Thanks,
Kim
kimstanely is offline   Reply With Quote
Old 05-10-2006, 03:26 PM   PM User | #11
Beagle
Senior Coder

 
Join Date: Jul 2005
Location: New York, NY
Posts: 1,084
Thanks: 4
Thanked 19 Times in 19 Posts
Beagle is an unknown quantity at this point
Kim,
You need to put the script I gave you in script tags in the head of the document.

Then you need to call the getImageByDayOfYear to get the image you want.

something like:

Code:
<body onload="document.getElementById('img1').src = getImageByDayOfYear((new Date()).getDayOfYear()).src;">
Beagle is offline   Reply With Quote
Old 05-10-2006, 05:05 PM   PM User | #12
kimstanely
New Coder

 
Join Date: Apr 2005
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
kimstanely is an unknown quantity at this point
hi,

Thanks beagle, it works but the first who images flash before they change. Also can i set it to work that the image change after 2 weeks.


Thanks for all the help.

Kim

PHP Code:
<html>
<
head>
<
script type="text/javascript">
var 
pic;

if (
document.images)
{
    
pic = new Array()

    
pic[0] = new Image(108108);
    
pic[0].src "flag_col.jpg";

    
pic[1] = new Image(108108);
    
pic[1].src "books_glasses.jpg";

    
pic[2] = new Image(108108);
    
pic[2].src "lawyers_steps.jpg";

    
pic[3] = new Image(108108);
    
pic[3].src "pen_book.jpg";

    
pic[4] = new Image(108108);
    
pic[4].src "scales1.jpg";
    
    
pic[5] = new Image(108,108);
    
pic[5].src 'ccs.gif'
    
    
pic[6] = new Image(108,108);
    
pic[6].src 'sj_e.gif'
}

function 
getImageByDayOfYear(p_day)
{
  if (
pic.length && pic.length 0)
  {
    return 
pic[(p_day 1)%pic.length];
  }
  else
  {
     return 
null;
   }
}

Date.prototype.getDayOfYear = function() {
  var 
this.getFullYear(), this.getMonth(), this.getDate();
  return (
Date.UTC(YMD) - Date.UTC(Y00)) / 86400000;
};

var 
doy = new Date();
var 
imageToDisplay getImageByDayOfYear(doy.getDayOfYear());

</script>
</head>

<body onload="document.getElementById('img1').src = getImageByDayOfYear((new Date()).getDayOfYear()).src;">

<img src="pen_book.jpg" id="img1">
</body>

</html> 
kimstanely is offline   Reply With Quote
Old 05-10-2006, 05:56 PM   PM User | #13
Beagle
Senior Coder

 
Join Date: Jul 2005
Location: New York, NY
Posts: 1,084
Thanks: 4
Thanked 19 Times in 19 Posts
Beagle is an unknown quantity at this point
you can modify the code to do whatever you want...

Right now, you're going off of the day of the year. You could divide that number by 14 rounded down to get every two weeks.

as for the flickering of the initial img, try not giving a source at all when you create it in the html. just <img src="" />

That might help.
Beagle is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:25 PM.


Advertisement
Log in to turn off these ads.