Vykonání JavaScriptu v určitý čas

js

Vykonání JavaScriptu v určitý čas
« kdy: 19. 09. 2012, 13:26:10 »
Potrebuju v presne dany cas vypsat JavaScriptem text a otevrit novou stranku. Skript tedy pobezi tak dlouho, nez nastane dany cas.

Zkousel jsem:
Kód: [Vybrat]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
<head>
  <meta http-equiv="content-language" content="cs" />
  <meta name="language" content="cs" />
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <title>:-)</title>
</head>
<body>
<script type="text/javascript">
var now = new Date();
var hour = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var i = 0;

//dany cas:
var hodiny = 13;
var minuty = 19;
var sekundy = 30;


while (i = 0) {
if(hour == hodiny && minutes == minuty && seconds == sekundy) {
document.getElementById("abc").innerHTML="TED<br>";
document.getElementById("abc").innerHTML=":-)";
window.open("http://www.google.cz",'_blank');
i = 1;
}
sleep(500);
delete now;
var now = new Date();
var hour = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
}
</script>

<p id="abc" style="font-size: 150%;"></p>
</body>
</html>

ale porad to nefunguje, nevite, kde delam chybu?
« Poslední změna: 19. 09. 2012, 22:42:29 od Petr Krčmář »


whata

Re:Vykonani JavaScriptu v urcity cas
« Odpověď #1 kdy: 19. 09. 2012, 13:36:44 »
A ja si furt rikam, proc mi prohlizec po probuzeni zere 100% procesoroveho casu :-/

Za a) = vs. ==

Za b) man setTimeout

js

Re:Vykonani JavaScriptu v urcity cas
« Odpověď #2 kdy: 19. 09. 2012, 13:52:35 »
Diky za radu. Upravil jsem to, ale stale to nefunguje:

Kód: [Vybrat]
<script type="text/javascript">
function ahojSvete() {
var now = new Date();
var hour = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var i = 0;

var hodiny = 13;
var minuty = 48;
var sekundy = 40;

while (i == 0) {
if(hour == hodiny && minutes == minuty && seconds == sekundy) {
document.write('TED');
document.getElementById("abc").innerHTML="TED<br>";
document.getElementById("abc").innerHTML=":-)";
window.open("http://www.google.cz",'_blank');
i = 1;
}
sleep(500);
delete now;
var now = new Date();
var hour = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
}
}

function spustit() {
    setTimeout('ahojSvete()', 300000); // 5 minut
}

spustit();
ahojSvete();
</script>

DK

Re:Vykonani JavaScriptu v urcity cas
« Odpověď #3 kdy: 19. 09. 2012, 14:11:29 »
za B!

javascript nema funkci sleep

Riff

Re:Vykonani JavaScriptu v urcity cas
« Odpověď #4 kdy: 19. 09. 2012, 14:18:19 »
Kód: [Vybrat]
function test(h, m, s, callback) {
    var now = new Date();
    var start = new Date();
    start.setHours(h, m, s);
    setTimeout(callback, start - now);
}

test(14, 15, 0, function() {
    document.getElementById("abc").innerHTML="TED<br />:-)";
    window.open("http://www.google.cz",'_blank');
});