��权限没有,则隐藏 function forum_list_access_filter($forumlist, $gid, $allow = 'allowread') { global $grouplist; if (empty($forumlist)) return array(); if (1 == $gid) return $forumlist; $forumlist_filter = $forumlist; $group = $grouplist[$gid]; foreach ($forumlist_filter as $fid => $forum) { if (empty($forum['accesson']) && empty($group[$allow]) || !empty($forum['accesson']) && empty($forum['accesslist'][$gid][$allow])) { unset($forumlist_filter[$fid]); } unset($forumlist_filter[$fid]['accesslist']); } return $forumlist_filter; } function forum_filter_moduid($moduids) { $moduids = trim($moduids); if (empty($moduids)) return ''; $arr = explode(',', $moduids); $r = array(); foreach ($arr as $_uid) { $_uid = intval($_uid); $_user = user_read($_uid); if (empty($_user)) continue; if ($_user['gid'] > 4) continue; $r[] = $_uid; } return implode(',', $r); } function forum_safe_info($forum) { //unset($forum['moduids']); return $forum; } function forum_filter($forumlist) { foreach ($forumlist as &$val) { unset($val['brief'], $val['announcement'], $val['seo_title'], $val['seo_keywords'], $val['create_date_fmt'], $val['icon_url'], $val['modlist']); } return $forumlist; } function forum_format_url($forum) { global $conf; if (0 == $forum['category']) { // 列表URL $url = url('list-' . $forum['fid'], '', FALSE); } elseif (1 == $forum['category']) { // 频道 $url = url('category-' . $forum['fid'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = url('read-' . trim($forum['brief']), '', FALSE); } if ($conf['url_rewrite_on'] > 1 && $forum['well_alias']) { if (0 == $forum['category'] || 1 == $forum['category']) { $url = url($forum['well_alias'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = ($forum['threads'] && $forum['brief']) ? url($forum['well_alias'] . '-' . trim($forum['brief']), '', FALSE) : url($forum['well_alias'], '', FALSE); } } return $url; } function well_forum_alias() { $forumlist = forum_list_cache(); if (empty($forumlist)) return ''; $key = 'forum-alias'; static $cache = array(); if (isset($cache[$key])) return $cache[$key]; $cache[$key] = array(); foreach ($forumlist as $val) { if ($val['well_alias']) $cache[$key][$val['fid']] = $val['well_alias']; } return array_flip($cache[$key]); } function well_forum_alias_cache() { global $conf; $key = 'forum-alias-cache'; static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc if (isset($cache[$key])) return $cache[$key]; if ('mysql' == $conf['cache']['type']) { $arr = well_forum_alias(); } else { $arr = cache_get($key); if (NULL === $arr) { $arr = well_forum_alias(); !empty($arr) AND cache_set($key, $arr); } } $cache[$key] = empty($arr) ? '' : $arr; return $cache[$key]; } ?>javascript - method not defined - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - method not defined - Stack Overflow

programmeradmin2浏览0评论
(function($){
    $.fn.slideshow = function(){

        function init(obj){

            setInterval("startShow()", 3000);           
        }               

        function startShow(){
            alert('h');
        }
        return this.each(function(){                    
            init(this);
        });

    }
})(jQuery);

i am getting error

startShow is not defined
(function($){
    $.fn.slideshow = function(){

        function init(obj){

            setInterval("startShow()", 3000);           
        }               

        function startShow(){
            alert('h');
        }
        return this.each(function(){                    
            init(this);
        });

    }
})(jQuery);

i am getting error

startShow is not defined
Share Improve this question asked Sep 17, 2010 at 10:20 coure2011coure2011 42.5k87 gold badges225 silver badges361 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 7

Change

setInterval("startShow()", 3000);            

To

setInterval(startShow, 3000);            

When you pass a string to setInterval(), the code inside is executed outside of the current scope. It's much more appropriate to just pass the function anyway. Functions can be passed around just like any other variable if you leave the parenthesis off.

If you need to pass a variable, you can use a solution similar to the one Guffa provided:

setInterval(function () { startShow(myVar); }, 3000);

This creates an anonymous function to be passed as the first argument to setInterval(), and within that anonymous function you have access to variables and functions further up the scope chain.

The startShow function is local to the scope, so when the setInterval method evaluates the string in the global scope, it won't find the function.

Use an anonymous function instead of the string:

setInterval(function(){ startShow(); }, 3000);

As the anonymous function use the locally declared function, a closure is created so that the function still has access to it.

 function startShow()
 {
        alert('h');
 }    
(function($){
        $.fn.slideshow = function(){

            function init(obj){

                setInterval("startShow()", 3000);           
            }               


            return this.each(function(){                    
                init(this);
            });

        }
    })(jQuery);

Your setting the time-out to look in the base scope, you need to create an anonymous function in the

Try the following:

(function($){
    $.fn.slideshow = function(){

        function init(obj){

            setInterval(function(){
                startShow();
            }, 3000);           
        }

        function startShow(){
           //Deprecated 
        }


        return this.each(function(){                    
            init(this);
        });

    }
})(jQuery);

Or you can create an object to hold your methods in:

(function($){
    $.fn.slideshow = function(){

        var Functions = {
            startShow : function(obj)
            {
                alert('Starting Show')
            }
        }

        function init(obj){

            setInterval(function(){
                 Functions.startShow(obj)
            }, 3000);           
        }

        return this.each(function(){                    
            init(this);
        });

    }
})(jQuery);

Passing a string to setInterval/setTimeout makes it run an eval() in the background. The function scope is lost in this eval and requires a global reference. Avoid the use of strings in setInterval/setTimeout , it's never needed and causes ambuigity.

(function($){
    $.fn.slideshow = function(){

        var init = function(obj){
            setInterval(startShow, 3000);           
        }               

        var startShow = function(){
            alert('h');
        }

        return this.each(function(){                    
            init(this);
        });

    }
})(jQuery);

This isn't directly relevant to your problem, but if you take a look at this page, you'll notice the best practices when creating your plugins methods:

http://docs.jquery./Plugins/Authoring

This is just an example:

(function( $ ){

  var methods = {
    init : function( options ) {
      setInterval(startShow(), 3000);
    },
    startShow : function( ) { 
      alert('h');
    }
  };

  $.fn.slideshow = function( method ) {

    // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    }    

  };

})( jQuery );
发布评论

评论列表(0)

  1. 暂无评论