I want to sleep my code for some seconds
how i can do that in javascript;
(like wait(1000); in java)
I have one for loop
for(var i=0;i<400;i++)
{
//i want here sleep() like function so that after one line drawn i want to wait for milliseconds
//then second line ,then third and so on (how to do this?)
drawLine(context,startx,starty,copyendx,copyendy);
}
i have used code:"setTimeout(function(){drawLine(context,startx,starty,copyendx,copyendy);},100);"
but its not working.
Last edited by tejasagawane; 09-14-2011 at 12:44 PM..
// here is the first part of the code
...
window.setTimeout(secondPart, 1000);
function secondPart() {
// the second part will be executed after 1000 milliseconds
...
}
I didn't got .Can you explain me with my code?
My code like this
Code:
for(var i=0;i<splitresponseData.length;i++)
{
//some delay (while calling drawLine() i want some delay for each "i"
// i want some delay before or after calling to drawLine() method.
drawLine(context,startx,starty,copyendx,copyendy);
}
for(var i=0;i<splitresponseData.length;i++)
{
//some delay (while calling drawLine() i want some delay for each "i"
// i want some delay before or after calling to drawLine() method.
setTimeout(function(){
drawLine(context,startx,starty,copyendx,copyendy);
}, 100* i );
}
__________________ my site (updated 5/13) STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
But it doesn't make sense to call drawLine() with the same parameters over and over again. I think (there is no evidence whatsoever from the OP) that also those parameters change with each loop ...
But it doesn't make sense to call drawLine() with the same parameters over and over again. I think (there is no evidence whatsoever from the OP) that also those parameters change with each loop ...
doh! yeah, that's an "I". fixed it via edit.
i assume that the comment lines above the function call alter the params passed to the draw function...
__________________ my site (updated 5/13) STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
I want to draw one line then wait for some milliseconds then draw next line again wait for next line and so on.
How i can do that in my code?
I have used setInterval() but its not working ,
I also tried sleep for some milliseconds but it also not working.
Please tell me whats wrong with it.
This is my code:
Quote:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var ms = 0;
var y=5;
var x=5;
var copyendx=0;
var copyendy=0;
var context;
var temp,total=0;
//var data=new Array(-1,3,-3,-7,5,1,-1,-3,2,5,7,7,7,7,1,3,3,4-8,5,6,6,7,7);
var data=new Array(-1,3,-3,-7,5,1,-1,-3,2,5,7,7,7,7,4,7,9,2,2,8);
//alert(data);
function init()
{
var graphCanvas = document.getElementById('graphSpace');
context =graphCanvas.getContext('2d');
drawgraph();
}
var startx=5+x;
var starty=150-(data[i]*y);
var endx=(5+x)+1;
var endy=150-(data[i]*y);
if(i==0)
{
copyendx=endx;
copyendy=endy;
startx=5+x;
starty=150;
}
//draw line and wait for some milliseconds
drawLine(context,startx,starty,copyendx,copyendy);
x=x+5;
// pausecomp(100);
copyendx=endx;
copyendy=endy;
}
}
//Draw line function
function drawLine(contextO, startx, starty, endx, endy) {
// var now = millis();
contextO.beginPath();
contextO.moveTo(startx, starty);
contextO.lineTo(endx, endy);
contextO.closePath();
contextO.stroke();
// ms=now;
}
//Sleep function
function pausecomp(millis)
{
var date = new Date();
var curDate = null;
do
{ curDate = new Date();
} while(curDate-date < millis);
}
function millis()
{
var d = new Date();
return d.getTime();
}