Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add animation in string

I want add rotation in my string for that i have set interval but its not getting me as i want. it works only for one time when i reload the browser. So what should i add for continuing the function.

this is my code HTML

<html> 
  <head>
  <title>JavaScript basic animation</title>
  <script type="text/javascript">
  </script>

  <script type="text/javascript" src="myfunction_2.js"></script>
  </head> <body onload="shubham()">
  <div id="target">w3resource</div>


<button >click</button>

  </body> 
  </html>

javascript

function shubham() 
{
var x=document.getElementById('target').textContent;

var y=x.split('');

var z=y[0];

var m=y[y.length-1];

setInterval(function () 
{
 document.getElementById('target').innerHTML = m+x.substring(0,8);
},500);


}
like image 365
naresh Avatar asked Feb 01 '26 23:02

naresh


1 Answers

The problem with your code was that the variables were initialized once, and whenever, the setInterval function was called, there was no change in variables and hence the result remained the same which appeared as function only executed 1 time only.

Move your variables inside the setInterval function.

setInterval(function() {
  var x = document.getElementById('target').textContent;

  var y = x.split('');

  var z = y[0];

  var m = y[y.length - 1];
  document.getElementById('target').innerHTML = m + x.substring(0, 8);
}, 500);
<div id="target">w3resource</div>
like image 121
Nikhil Aggarwal Avatar answered Feb 04 '26 14:02

Nikhil Aggarwal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!