Trouble getting JS to work
-
What I’m trying to do is have a number count-up on my page that increases at a steady increment/interval. I created stylized images to represent each number 0-9 in the count-up, so I also need to replace each number with its respective image. The first Function in the code represents that task, and the second script is the actual count-up. I’m using wordpress so I’ve added the JS file and enqueued it in the header.php file.
Here’s where I had my problem: I was able to get the count-up to work, but the counterimages(input) function doesn’t want to work for me. It might be an issue with how I am trying to “call” on the function on the WordPress page. Or could it be that I need to separate the two scripts into two js files? A solution would be wonderful as I have looked all over and read the codexes often.
If anyone could help me out I would be very grateful!
function counterimages(input) { var output = "" for (var i = 0; i < input.length; i++) { var chr = input.substring(i, i + 1) if (chr == '£') { output += '<img border="0" src="img/pound.gif">'; } else if (chr == '.') { output += '<img border="0" src="img/dot.gif">'; } else { output += '<img border="0" src="http://eatiply3.staging.wpengine.com/wp-content/uploads/2013/05/'+(chr+1)+'.png">'; } return output; } var START_DATE = new Date("October 21, 2012 22:30:00"); // put in the starting date here var INTERVAL = 1; // refresh interval in seconds var INCREMENT = 769.2; // increase per tick (1/0.0013 ~ 769) var START_VALUE = 35000; // initial value when it's the start date var count = 0; jQuery(document).ready(function($) { var msInterval = INTERVAL * 1000; var now = new Date(); count = parseInt((now - START_DATE)/msInterval) * INCREMENT + START_VALUE; $('#counter').html(count.toFixed(0)); window.setInterval( function(){ count += INCREMENT; $('#counter').html(count.toFixed(0)); }, msInterval); });
The topic ‘Trouble getting JS to work’ is closed to new replies.