Reputation: 137
I have a simple chunk of code to draw a line in a page. My problem is that I don't know much about HTML5 or JS and I need help to set a delay on the drawing of this line. I want to be able to choose if I want to see it drawing instantly when opening the page or define it to have 5 seconds delay before being draw.
Here it is:
<canvas id="myCanvas" width="1250" height="120"></canvas>
<script>
var canvas = $("#myCanvas")[0];
var c = canvas.getContext("2d");
var amount = 0;
var startX = 164;
var startY = 120;
var endX = 1094;
var endY = 120;
setInterval(function() {
amount += 0.01; // change to alter duration
if (amount > 1) amount = 1;
c.clearRect(0, 0, canvas.width, canvas.height);
c.strokeStyle = "black";
c.lineWidth=1;
c.strokeStyle="#707070";
c.moveTo(startX, startY);
// lerp : a + (b - a) * f
c.lineTo(startX + (endX - startX) * amount, startY + (endY - startY) * amount);
c.stroke();
}, 0);
</script>
Thank you for the help!
Upvotes: 2
Views: 12899
Reputation: 6911
Short answer
Use setTimeout
function to delay execution. What setTimeout
does is set up a timer which will execute specified function after specified amount of time. E.g.
setTimeout(function() {
alert("Hello!");
}, 5000);
will show an alert after 5 seconds (note that time is specified in milliseconds).
Long answer
There are two functions that allow you to schedule a function execution.
Both functions can be canceled using their counterparts (clearTimeout
and clearInterval
).
Upvotes: 0
Reputation: 58595
You need to use setTimeout
:
setTimeout(function() {
setInterval(function() {
amount += 0.01; // change to alter duration
if (amount > 1) amount = 1;
c.clearRect(0, 0, canvas.width, canvas.height);
c.strokeStyle = "black";
c.lineWidth=1;
c.strokeStyle="#707070";
c.moveTo(startX, startY);
// lerp : a + (b - a) * f
c.lineTo(startX + (endX - startX) * amount, startY + (endY - startY) * amount);
c.stroke();
}, 0);
}, 5000);
Upvotes: 2
Reputation: 3650
You need to use setTimeout. setTimeout runs a call after a certain delay.
The function used in your script, setInterval, runs the same function over and over again at a certain interval. Strile's answer should help you
Upvotes: 0
Reputation: 239250
Wrap your setInterval
call in a setTimeout
call. setInterval
invokes its function argument repeatedly, where the second argument specifies the delay between invocations. setTimeout
invokes its function argument once, after the delay has passed.
function redraw() {
amount += 0.01; // change to alter duration
if (amount > 1) amount = 1;
c.clearRect(0, 0, canvas.width, canvas.height);
c.strokeStyle = "black";
c.lineWidth=1;
c.strokeStyle="#707070";
c.moveTo(startX, startY);
// lerp : a + (b - a) * f
c.lineTo(startX + (endX - startX) * amount, startY + (endY - startY) * amount);
c.stroke();
}
setTimeout(function () { setInterval(redraw, 0) }, 5000);
Upvotes: 2
Reputation: 5781
Wrap it in a setTimeout:
var canvas = $("#myCanvas")[0];
var c = canvas.getContext("2d");
var amount = 0;
var startX = 164;
var startY = 120;
var endX = 1094;
var endY = 120;
setTimeout(function() {
var interval = setInterval(function() {
amount += 0.01; // change to alter duration
if (amount > 1) {
amount = 1;
clearInterval(interval);
}
c.clearRect(0, 0, canvas.width, canvas.height);
c.strokeStyle = "black";
c.lineWidth=1;
c.strokeStyle="#707070";
c.moveTo(startX, startY);
// lerp : a + (b - a) * f
c.lineTo(startX + (endX - startX) * amount, startY + (endY - startY) * amount);
c.stroke();
}, 0);
}, 3000);
The above waits 3 seconds (3000 milliseconds) before starting the drawing. Also, whenever you start an interval with setInterval you should store the return value so you can stop the interval later. The code above stops the interval when it's done drawing with clearInterval().
Upvotes: 7