Jump to content
moubik

javascript sleep

Recommended Posts

vreau sa fac un sleep in javascript dar fara sa-mi duca procesorul la 100%

ca in exemplul:


function pause(millis)
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < millis)
}

si nici metoda asta nu ma ajuta, pentru ca nu stiu de cate ori se va parcurge loop-ul:


setTimeout('nextpieceofcode();', 5000);

pe net nu am gasit rezolvari diferite de cele 2

Link to comment
Share on other sites

Ce am gasit aici: http://forums.codewalkers.com/client-side-things-82/javascript-sleep-82584.html ,dar nu am testat:


function pause(numberMillis)
{
var now = new Date();
var exitTime = now.getTime() + numberMillis;
while (true)
{
now = new Date();
if (now.getTime() > exitTime)
return;
}
}

Pe de alta parte, ai putea sa incerci VBScript:


<script language='vbscript'>
WScript.Sleep(1000)
</script>

Bafta !!!

Link to comment
Share on other sites

ms vladiii

nu stiam exact cum sa integrez vbscriptul asa ca pana la urma am gasit o solutie.

de exemplu am functia


//puneti orice valoare aici ca sa nu dea eroare cand face primul clearTuneOut
//sincer nu stiu ce se poate intampla daca faci unset la o variabila care ar putea fi importanta
var rec = 1
function recursiva()
{
//yeah, i'm a programmer
//codul vine aici
//si ca sa fac delay pot sa fac asa
//golesc variabila de timeout
clearTimeout(rec);
//pui aici conditia de stop
if (amterminat != true)
{
rec = setTimeOut("recursiva()", 500);
}
}

Link to comment
Share on other sites

ok, am reusit sa fac ce doream dar am dat de alta problema si nu inteleg de ce se intampla asa.

am codul:

<head>

<script>

theshit = "Ma enerveaza ca nu merge pe Firefox. Stie cineva de ce ?";
doingshit = 5;

bucket = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890!<>'();/=+-*";
bucket = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890!.|_?";
//bucket = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";

function start()
{
//create junk,
var whereto = document.getElementById('text');
var tempax = "";
for (i=0 ; i<theshit.length ; i++)
{
tempax = tempax + bucket.charAt(Math.floor (Math.random() * (bucket.length)));
}
whereto.innerHTML = tempax;
startToDoShit();
}

function startToDoShit()
{
var whereto = document.getElementById('text');
var tempax = "";
var there = whereto.innerHTML;
var modificari = 0;
for (i=0 ; i<theshit.length ; i++)
{
if (there.charAt(i) != theshit.charAt(i))
{
tempax = tempax + bucket.charAt(Math.floor (Math.random() * (bucket.length)));
modificari = 1;
}
else
{
tempax = tempax + theshit.charAt(i);
}
}

whereto.innerHTML = tempax;
tempax = "";
clearTimeout(doingshit);
if (modificari == 1) doingshit = setTimeout("startToDoShit()", 10);
}

function scrapheap()
{
document.getElementById('text').innerHTML=String.fromCharCode(Math.floor (Math.random() * (255)));
}

//
</script>
</head>

<body onload="start()">
<font face="courier">
<div id="text">nu ar trebui sa existe</div>
</font>
</body>

faza este ca merge pe opera si pe internet explorer, dar pe firefox nu merge.

daca este sa scot liniile <font face="courier"> si </font> merge si pe firefox, dar nu arata asa cum doresc eu :)

stie cineva de ce?

Link to comment
Share on other sites

<head>

<script>

theshit = "Acum ar trebui sa mearga si pe Firefox. Spune-mi daca-ti merge. KONKHRA";
doingshit = 5;

bucket = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890!<>'();/=+-*";
bucket = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890!.|_?";
//bucket = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";

function start()
{
//create junk,
var whereto = document.getElementById('text');
var tempax = "";
for (i=0 ; i<theshit.length ; i++)
{
tempax = tempax + bucket.charAt(Math.floor (Math.random() * (bucket.length)));
}
whereto.innerHTML = tempax;
startToDoShit();
}

function startToDoShit()
{
var whereto = document.getElementById('text');
var tempax = "";
var there = whereto.innerHTML;
var modificari = 0;
for (i=0 ; i<theshit.length ; i++)
{
if (there.charAt(i) != theshit.charAt(i))
{
tempax = tempax + bucket.charAt(Math.floor (Math.random() * (bucket.length)));
modificari = 1;
}
else
{
tempax = tempax + theshit.charAt(i);
}
}

whereto.innerHTML = tempax;
tempax = "";
clearTimeout(doingshit);
if (modificari == 1) doingshit = setTimeout("startToDoShit()", 10);
}

function scrapheap()
{
document.getElementById('text').innerHTML=String.fromCharCode(Math.floor (Math.random() * (255)));
}

//
</script>
<style type="text/css">
<!--
.style1 {font-family: "Courier New", Courier, monospace}
-->
</style>
</head>

<body onLoad="start()">
<div class="style1" id="text">nu ar trebui sa existe</div>
</body>

Spune-mi daca-ti merge. Good luck!

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
Reply to this topic...

×   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.



×
×
  • Create New...