Wednesday, 15 May 2013

javascript - settimeout giving Uncaught ReferenceError: function is not defined -



javascript - settimeout giving Uncaught ReferenceError: function is not defined -

could tell me why gives error?

i moved code functions allow me delay it's not sensitive (was getting annoying)

uncaught referenceerror: hideleftnav not defined

uncaught referenceerror: showleftnav not defined

function showleftnav() { $(".leftnavdiv").css('width','500px'); $("body").css('padding-left','510px'); //get measurements of window var mywidth = 0, myheight = 0; if( typeof( window.innerwidth ) == 'number' ) { //non-ie mywidth = window.innerwidth; myheight = window.innerheight; } else if( document.documentelement && ( document.documentelement.clientwidth || document.documentelement.clientheight ) ) { //ie 6+ in 'standards compliant mode' mywidth = document.documentelement.clientwidth; myheight = document.documentelement.clientheight; } else if( document.body && ( document.body.clientwidth || document.body.clientheight ) ) { //ie 4 compatible mywidth = document.body.clientwidth; myheight = document.body.clientheight; } $('#maindiv').width(mywidth - 540); } function hideleftnav() { $(".leftnavdiv").width(10); $("body").css('padding-left','20px'); //get measurements of window var mywidth = 0, myheight = 0; if( typeof( window.innerwidth ) == 'number' ) { //non-ie mywidth = window.innerwidth; myheight = window.innerheight; } else if( document.documentelement && ( document.documentelement.clientwidth || document.documentelement.clientheight ) ) { //ie 6+ in 'standards compliant mode' mywidth = document.documentelement.clientwidth; myheight = document.documentelement.clientheight; } else if( document.body && ( document.body.clientwidth || document.body.clientheight ) ) { //ie 4 compatible mywidth = document.body.clientwidth; myheight = document.body.clientheight; } $('#maindiv').width(mywidth - 50); } $(".leftnavdiv").live({ //code autohide mouseenter: function () { settimeout("showleftnav()", 5000); }, mouseleave: function () { settimeout("hideleftnav()", 5000); } });

looks you've found 1 problem using settimeout string first argument. here's condensed illustration illustrating same problem:

(function() { function test() { console.log('test'); } settimeout('test()', 500); // referenceerror: test not defined settimeout(test, 500); // "test" settimeout(function() { // "test" test(); }), 500); })();

demo: http://jsfiddle.net/mxemc/1/

using string causes code evaluated window context. since code in callback function, test isn't accessible window; it's private , restricted scope of anonymous function.

referencing function test avoids problem because you're pointing straight function without using eval.

javascript jquery settimeout

No comments:

Post a Comment