function doSomethingWith(param)
{
document.body.addEventListener(
'scroll',
function()
{
document.write(param);
},
false
); // An event that I want to remove later
}
setTimeout(
function()
{
document.body.removeEventListener('scroll', HANDLER ,false);
// What HANDLER should I specify to remove the anonymous handler above?
},
3000
);
doSomethingWith('Test. ');
Run Code Online (Sandbox Code Playgroud) 我正在学习Python.希望有人指出我正确的方式.
这就是我想在下面做的事情:
def decorate(function):
def wrap_function(*args, **kwargs):
str = 'Hello!' # This is what I want
return function(*args, **kwargs)
return wrap_function
@decorate
def print_message():
# I'd like to pass 'str' as mentioned above
# to any functions' argument like below:
print(str) # 'str' is same as above
Run Code Online (Sandbox Code Playgroud)
任何的想法?提前致谢.
我目前正在编写JavaScript并且对回调感到困惑.我发现它不是那种内置函数...
我现在正在阅读O'Relly JavaScript第5版,它显示的示例代码如下所示:
getText = function(url, callback) // How can I use this callback?
{
var request = new XMLHttpRequest();
request.onreadystatechange = function()
{
if (request.readyState == 4 && request.status == 200)
{
callback(request.responseText); // Another callback here
}
}
request.open('GET', url);
request.send();
}
Run Code Online (Sandbox Code Playgroud)
基本上,我想我不明白一般的想法callback......有人可以写一个示例代码来利用callback上述优势吗?
我是JavaScript编程的新手.我现在正在处理我的Google Chrome扩展程序.这是不起作用的代码......:P
我希望getURLInfo函数返回其JSON对象,并希望将其放入resp.有人可以修改我的代码让它工作吗?
function getURLInfo(url)
{
var xhr = new XMLHttpRequest();
xhr.open
(
"GET",
"http://RESTfulAPI/info.json?url="
+ escape(url),
true
);
xhr.send();
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4)
{
return JSON.parse(xhr.responseText);
}
}
}
var resp = getURLInfo("http://example.com/") // resp always returns undefined...
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我正在使用谷歌Chrome 10并编写JavaScript来检测滚动结束.
要检测滚动结束window,下面的代码工作正常:
window.addEventListener(
'scroll',
function()
{
var scrollTop = document.documentElement.scrollTop ||
document.body.scrollTop;
var offerHeight = document.body.offsetHeight;
var clientHeight = document.documentElement.clientHeight;
if (offsetHeight <= scrollTop + clientHeight)
{
// Scroll end detected
}
},
false
);
Run Code Online (Sandbox Code Playgroud)
现在我想检测指定元素的滚动结束,例如<section id="box" style="height: 500px; overflow: auto;">
这是无法正确检测的代码:
document.getElementById('box').addEventListener(
'scroll',
function()
{
var scrollTop = document.getElementById('box').scrollTop;
var offerHeight = document.getElementById('box').offsetHeight;
var clientHeight = document.getElementById('box').clientHeight;
if (offsetHeight <= scrollTop + clientHeight)
{
// This is called before scroll end!
}
},
false
); …Run Code Online (Sandbox Code Playgroud) 我现在正在尝试使用Browserify,我遇到了问题.
我总是使用Backbone和Lodash而不是Underscore,所以我为Browserify写了一些shim脚本:
shims/lodash.js:
'use strict';
/* global window,require,module */
require('../vendor/lodash.underscore-1.2.0');
module.exports = window._;
Run Code Online (Sandbox Code Playgroud)
shims/backbone.js:
'use strict';
/* global window,require,module */
require('../vendor/backbone-1.0.0');
module.exports = window.Backbone;
Run Code Online (Sandbox Code Playgroud)
app.coffee:
'use strict'
$ = require './shims/jquery'
_ = require './shims/underscore'
Backbone = require './shims/backbone'
Run Code Online (Sandbox Code Playgroud)
我实际上grunt-coffeeify用来构建Browserify模块,它在下面说错误:
Running "coffeeify:source" (coffeeify) task
Warning: module "underscore" not found from "/Users/User/proj/src/js/vendor/backbone-1.0.0.js" Use --force to continue.
Run Code Online (Sandbox Code Playgroud)
我应该更改什么才能正常工作Backbone?提前致谢.
UPDATE
不知何故,它适用于以下代码:
shims/lodash.js:
'use strict';
/* global require,module */
module.exports = require('../vendor/lodash-1.2.0');
Run Code Online (Sandbox Code Playgroud)
shims/backbone.js:
'use strict';
/* global …Run Code Online (Sandbox Code Playgroud) 我现在将我的小型Google App Engine应用程序迁移到Heroku平台.我实际上并没有使用Bigtable,并且webapp2大大降低了我的迁移成本.
现在我一直坚持处理静态文件.
有没有好的做法?如果是的话,请带我到那里.
提前致谢.
编辑
好吧,我现在正在使用paste我的WSGI服务器.而且paste.StaticURLParser()应该是什么我需要实现静态文件处理程序.但是我不知道如何将它与它集成webapp2.WSGIApplication().谁能帮助我?
也许我需要覆盖webapp2.RequestHandler类才能paste.StaticURLParser()正确加载;
import os
import webapp2
from paste import httpserver
class StaticFileHandler(webapp2.RequestHandler):
u"""Static file handler"""
def __init__(self):
# I guess I need to override something here to load
# `paste.StaticURLParser()` properly.
pass
app = webapp2.WSGIApplication([(r'/static', StaticFileHandler)], debug=True)
def main():
port = int(os.environ.get('PORT', 5000))
httpserver.serve(app, host='0.0.0.0', port=port)
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激!
javascript ×4
python ×2
asynchronous ×1
backbone.js ×1
browserify ×1
callback ×1
closures ×1
decorator ×1
heroku ×1
lodash ×1
scroll ×1
static-files ×1
webapp2 ×1