$( document ).ready( function() {
// just screwing around ;)
$( '.example' ).each( function(){
var
colorR = Math.floor( 128 + 2 * ( Math.random() * 64 ) ),
colorG = Math.floor( 128 + 2 * ( Math.random() * 64 ) ),
colorB = Math.floor( 128 + 2 * ( Math.random() * 64 ) );
$( this ).css( "background-color", "rgb(" + colorR + "," + colorG + "," + colorB + ")" );
});
/**
* Basic JS exception usage
*
*/
$( '.example-1-btn' ).click( function(){
var txtEl = '.example-1-txt';
$( txtEl ).empty();
var speed = $( '.example-1-input' ).val();
//trying to execute some code
try {
if ( speed == "" ) {
throw "Nic nie wpisałeś !";
}
if ( isNaN( speed ) ) {
throw "Nie wpisałeś liczby!";
}
speed = Number( speed );
if ( speed > 16 ) {
throw "Trochę za dużo, nie sądzisz? ...";
}
if ( speed < 14 ) {
throw "Troche więcej wiary w te małe dinozaury! :D";
}
throw "Tak, tyle mniej więcej potrafią wyciągnąć w krótkim sprincie. #funfact";
}
// if something went wrong then catch error
catch( error ) {
// and display it
$( txtEl ).append( error );
}
// we can also run some code regardless if there was an exception thrown or not
finally {
//clear input
$( '.example-1-input' ).val('');
}
});
/**
* We can nest try-catch blocks
*
*/
$( '.example-2-btn' ).click( function(){
$( '.example-2-txt' ).empty();
try{
try{
throw new Error( "siemaneczko!" );
}
catch( inner_error ){
//throw "wow!"; //uncomment this line and comment next one to change type of thrown exception
throw new Error( "wow!" ); // this will throw Error object with given message instead of string that was thrown in line above
$( '.example-2-txt' ).append( inner_error.message ); // this line and everything below in this scope wont be reached because an...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.