Rotate currencies with jQuery
I'm looking for a simple script that rotate currencies with a 1 second interval on all spans with a class = value.
<script type="text/javascript">
$(document).ready(function () {
var str1 = "£";
var str2 = "€";
var str3 = "$";
$("span.value").text(str1); //how can I rotate between the str1, 2 and 3 with a 1 second interval?
});
</script>
HTML looks something like this:
<h2>Get <span class="value"><开发者_运维问答/span> discount.</h2>
<h3>Get <span class="value"></span> extra.</h3>
and so on....
All help is appreciated!
The following will hopefully suffice:
$(document).ready(function () {
var currencyIndex = 0;
var currencies = ['£', '€', '$'];
var cInterval = setInterval(function() {
$("span.value").text(function() {
return currencies[currencyIndex];
});
currencyIndex = (currencyIndex + 1) % currencies.length;
}, 1000);
});
Remember to use entities in HTML, not the symbols or you will get odd results.
You could use the setInterval function:
var currencies = ['£', '€', '$'];
var index = 0;
window.setInterval(function() {
var value = currencies[(index++) % currencies.length];
$('span.value').text(value);
}, 1000);
I would advise making them an array:
var i;
window.setInterval(function() {
$('span.value').text(currencies[(++i % currencies.length) + 1]);
},1000)
精彩评论