javascript - Greasemonkey script seems to ignore setInterval? -


i'm trying make toy script in greasemonkey cause screen repeatedly jump top of screen when click button, , stop jumping when click button again.

this code:

var perpetualscroll = function () {     var scrolling = false;      var scroll = function () {         if (scrolling) {             window.scrollto(0, 0);         }     };      var scrolldiv = document.createelement("div");     scrolldiv.id = "topscroll0x2a";     scrolldiv.innerhtml = '<a class="topscroll" onclick="scrolling = !scrolling;" style="display:block; position:fixed; bottom: 1em; right: 1em; color:#fff; background-color:#000; padding:.5em;" href="#">start scroll</a>';     document.body.appendchild(scrolldiv);      var intervalid = window.setinterval(scroll, 50); };  perpetualscroll(); 

when click button in lower corner script creates, jump top of screen, doesn't continue perpetually so.

i'm new javascript , greasemonkey, i'm not quite sure problem is. suspect might due issues in onclick part of link, if is, can't seem figure out.

doing onclick not work expect. innerhtml string, js has no idea scoped within perpetualscroll function.

onclick handlers strings evaluated in global scope, have equivalent this:

 window.scrolling = !window.scrolling; 

the scrolling variable want different.

you should create actual function this:

var = document.createelement('a'); a.classname = (a.classname || "") + ' topscroll'; a.style.display = 'block'; a.style.position = 'fixed'; a.style.bottom = '1em'; a.style.right = '1em'; a.style.color = '#fff'; a.style.backgroundcolor = '#000'; a.style.padding = '0.5em'; a.href = '#'; a.onclick = function(e){     scrolling = !scrolling;     return false; }; scrolldiv.appendchild(a); 

obviously setting css terrible, should put in separate stylesheet anyway.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -