javascript - Pick subset of functions as string & evaluate -


i have set of defined js functions. want users pick subset of these using comma separated string. want randomly pick subset, , evaluate function selected. have 99% working, except not evaluating reason. why console keep telling me 'undefined not function'?

take peek @ line 37: http://jsfiddle.net/ftaq8q4m/

// 1 user picks subset of function names comma seperated string. var effectsubset = 'func4, func5, func6';   // 2 available functions     function func1() {         $('html').append('func1');     }      function func2() {         $('html').append('func2');     }      function func3() {         $('html').append('func3');     }      function func4() {         $('html').append('func4');     }      function func5() {         $('html').append('func3');     }      function func6() {         $('html').append('func4');     }   var autoplay = function () {     // 3 user's subset of functions, turn array     effectarray = effectsubset.split(',');     // 4 pick random function array     var effect = effectarray[math.floor(math.random() * effectarray.length)];     // 5 run randomly picked function     window[effect](); }  // 6 re-run every second var playinterval = setinterval(autoplay, 1000); 

i see 2 things wrong:

  1. your functions not assigned window
  2. your "effect" variable contained leading whitespace

i have corrected above points here: http://jsfiddle.net/ftaq8q4m/1/

this appears have resolved issue described.

example:

window.func1 = function() {     $('html').append('func1'); } 

and:

window[effect.trim()](); 

update

as bonus fixed misleading append messages =d

http://jsfiddle.net/ftaq8q4m/5/


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

jdbc - Not able to establish database connection in eclipse -