I’ve appropriated some code to create a countdown timer for the launch of a new site. The timer itself, which uses a custom typeface, works very well.
However, when the if / else statement for timesup is true and goes to ‘else’, I can’t get any other function to work apart from the way you see it below:
function formatresults(){
if (this.timesup==false){//if target date/time not yet met
var displaystring="<div class='row'><div class='col'> "+arguments[0]+" </div> <div class='thincol'>•</div> <div class='col'> "+arguments[1]+" </div> <div class='thincol'>•</div><div class='col'> "+arguments[2]+" </div> <div class='thincol'>•</div> <div class='col'> "+arguments[3]+" </div> </div>"
}
else {
var displaystring = "BAR"
}
return displaystring
}
if I change var displaystring to window.location (to load the full site at the end of the countdown), the timer ceases to work completely.
I know this is due to a fundamental misunderstanding of javascript in a way, but in all honesty I’ve developed sites using javascript and libraries like jquery and mootools and haven’t hit a brick wall like this in a long time.
If you have any advice, please do share it with me!
Many thanks in advance
Seems like you have a serious issue of no formatting madness going on. You should properly indent and format your JavaScript and terminate each statement with a semicolon:
The above code should work just fine. Now I don’t know why you would want to redirect the user after having set
displaystring, since that will abort the current page execution, but nonetheless; the above code should work.As Ben Lee writes; you shouldn’t be doing
window.location.hrefchanges in this function, but instead in the function invokingformatresults()soformatresults()only does what it says, which is to format the results. This is how I’d solve it: