Jump to content
php.lv forumi
  • 0

JavaScript taimeri


J-EX

Question

Sveiki!

 

Vārdu sakot uzdevums ir tāds, ka lapā ir daudz taimeru, kuru sākuma vērtības norāda PHP skripts, notiek laika atskaite, zem katra taimera ir poga... nospiežot pogu, ar ajax datubāzē tiek palielināts laiks par noteiktu intervālu un atjaunots taimeris...

 

problēma ir tāda, ka taimeris neatjaunojas, datubāzē man viss pareizi ierakstās, bet kad vēlreiz palaižu taimeri, tas turpina atskaitīt laiku no momenta, pirms pogas nospiešanas... pārlādējot lapu taimeris atkal rāda pareizo laiku...

 

Rekur būs kods

 

function countdown(hour, minute, secunde, num)

{

Time_Left = secunde + (minute*60) + (hour*3600);

 

if(Time_Left < 0) Time_Left = 0;

 

//More datailed.

hours = Math.floor(Time_Left / 3600);

Time_Left %= (3600);

minutes = Math.floor(Time_Left / 60);

Time_Left %= 60;

seconds = Time_Left;

/////// liekam klaat nulles

if(minutes<10) minutes = '0'+minutes;

if(seconds<10) seconds = '0'+seconds;

///////////////////////////

document.getElementById("countdown"+num).innerHTML = hours + ' : ';

document.getElementById("countdown"+num).innerHTML += minutes + ' : ';

document.getElementById("countdown"+num).innerHTML += seconds;

 

Time_Left--;

hours = Math.floor(Time_Left / 3600);

Time_Left %= (3600);

minutes = Math.floor(Time_Left / 60);

Time_Left %= 60;

seconds = Time_Left;

 

 

//Recursive call, keeps the clock ticking.

setTimeout('countdown(' + hours + ',' + minutes + ',' + seconds +',' + num + ');', 1000);

}

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Nē, es iedevu tieši to ko vajag, tu vienkārši neieraudzīji to, uz ko bija tēmēts :)

 

hmm... bet uz ko bija tēmēts? :) uz Clear interval, ja? papromēju, tas pats... funkciju palaižu ar pirno, kas izsauc otro, otrā ieciklējas un notiek laika atskaite, bet kad izsaucu vēlreiz pirmo ar jaunām laika vērtībām... laiks neatjaunojas... Lūdzu glābjiet! :D

 

================================================

 

var t;

function countdown_clock(hour, minute, secunde, num)

{

//I chose a div as the container for the timer, but

//it can be an input tag inside a form, or anything

//who's displayed content can be changed through

//client-side scripting.

window.clearTimeout(t);

countdown(hour, minute, secunde, num);

}

 

function countdown(hour, minute, secunde, num)

{

Time_Left = tmp = secunde + (minute*60) + (hour*3600);

 

if(Time_Left < 0) Time_Left = 0;

 

//More datailed.

hours = Math.floor(Time_Left / 3600);

Time_Left %= (3600);

minutes = Math.floor(Time_Left / 60);

Time_Left %= 60;

seconds = Time_Left;

/////// liekam klaat nulles

if(minutes<10) minutes = '0'+minutes;

if(seconds<10) seconds = '0'+seconds;

///////////////////////////

document.getElementById("countdown"+num).innerHTML = hours + ' : ';

document.getElementById("countdown"+num).innerHTML += minutes + ' : ';

document.getElementById("countdown"+num).innerHTML += seconds;

 

Time_Left = tmp-1;

hours = Math.floor(Time_Left / 3600);

Time_Left %= (3600);

minutes = Math.floor(Time_Left / 60);

Time_Left %= 60;

seconds = Time_Left;

 

 

//Recursive call, keeps the clock ticking.

t = setTimeout('countdown(' + hours + ',' + minutes + ',' + seconds +',' + num + ');', 1000);

}

Link to comment
Share on other sites

  • 0

Realizācija diezgan aplama:

 

1) Pietiek ar vienu taimera izsaukšanu;

2) Kāpēc neizmantot intervālu?

 

//inicializējam sākuma laikus, cik palicis:

t={"15":500,"16":600,"17":200}
o=new Date();
st=o.getTime();  //aprēķinām sākuma laiku


function timer(){
var o=new Date();
var tt=o.getTime();
for(var id in t){
 var dt=Math.round((tt- st)/1000);  // iegūstam laika starpību
 var tl=t[id]-dt;  // aprēķinam atlikušo laiku
 $('#laiks_'+id).html(noformejam_smuku_laiku(tl));
}
}

setInterval(timer(),1000);

 

tas uz ātro princips, tālāk vēl jāsaliek pārbaudes uz nulli un pie ajax atbildes, palielina attiecīgo laiku t[id]+=10;

Edited by codez
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...